Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Casting and low-level types

8 views
Skip to first unread message

Geoffrey Broadwell

unread,
Feb 27, 2007, 9:26:18 AM2/27/07
to p6l
What happens when you cast between low-level types? If the source value
is out of range of the destination type, do you get:

1. An exception?
2. Clip to finite range always?
3. Clip to finite range for ints, clip to infinities for nums?
4. Exception when dest is int, clip to infinities when dest is num?
5. Copy bits that fit from source to dest and reinterpret?

Personally, I think option 1 or option 4 make the most sense for
conversion between int, uint, num, and complex types, while either
option 1 or option 5 make sense for conversion to/from buf types.

Also, when casting from a num type to an int type, is there a way to
specify desired rounding/truncation behavior in a way that allows the
most efficient code under the covers, rather than making a side trip
through Num and Int?


-'f


Larry Wall

unread,
Feb 27, 2007, 7:27:30 PM2/27/07
to p6l
On Tue, Feb 27, 2007 at 06:26:18AM -0800, Geoffrey Broadwell wrote:
: What happens when you cast between low-level types? If the source value

: is out of range of the destination type, do you get:
:
: 1. An exception?
: 2. Clip to finite range always?
: 3. Clip to finite range for ints, clip to infinities for nums?
: 4. Exception when dest is int, clip to infinities when dest is num?
: 5. Copy bits that fit from source to dest and reinterpret?
:
: Personally, I think option 1 or option 4 make the most sense for
: conversion between int, uint, num, and complex types, while either
: option 1 or option 5 make sense for conversion to/from buf types.

Basically it's 4, except that the exception is a warning (which is
a form of resumable exception in Perl 6.)

: Also, when casting from a num type to an int type, is there a way to


: specify desired rounding/truncation behavior in a way that allows the
: most efficient code under the covers, rather than making a side trip
: through Num and Int?

Depends on what you call to perform the rounding. Could be anything
from a macro to a multimethod. The default round() is presumably a
multimethod on Num that produces an Int, but you can always define
more specific multis or functions or macros, or whack the compiler
upside the head with a pragma.

Larry

0 new messages