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

Int-to-Num autocoercion

8 views
Skip to first unread message

Larry Wall

unread,
Jan 30, 2007, 12:50:59 PM1/30/07
to perl6language
On Tue, Jan 30, 2007 at 06:02:59PM +0100, TSa wrote:
: How are coercions handled when calling functions?
:
: sub identity ( Int $i ) { return $i }
:
: my Num $x = 3.25;
:
: say indentity($x); # prints 3 or type error? Or even 3.25?
:
: I'm opting for type error on the footing that Int <: Num and
: you can't allow a supertype where a subtype is expected.

Num-to-Int autocoercion is an explicit exception built into the
language. Perl 5 programmars would lynch us if we broke it. But yes,
it's basically cheating. We also cheat and autocoerce Str-to-Num
and Num-to-Str all over the place. :)

The interesting question is how this autocoercion is best declared.
We've batted around several ideas on that in the past, but the general
feeling is that it should only be applied to these basic everyday types,
and that exotic types should not generally autocoerce unless you've
explicitly declared a multi to handle the situation.

Larry

TSa

unread,
Jan 30, 2007, 3:07:13 PM1/30/07
to perl6language
HaloO Larry,

you wrote:
> Num-to-Int autocoercion is an explicit exception built into the
> language. Perl 5 programmars would lynch us if we broke it. But yes,
> it's basically cheating.

In your array subscript reply you conceded that flooring
is better behaved than truncation. Which would mean that
-4 === Int(-3.25) should be True. This is important if the
right hand side is an auto-coerced parameter:

sub foo (Int $x)
{
if $x === -4 { say "floored" }
}

foo(-3.25);

sub distance (Int $from, Int $to) { return $to - $from }

say distance(-3.3, 2.3); # should print 6 or perhaps 5.6

The 5.6 return value would somehow maintain the type
prior to the auto-coercion and not violate the unspecified
return value of foo. That is, one needs a sig of :(Int,Int-->Int)
to also force an Int return value. But then that would be
floored to 5. So one way or another you loose precision
when making the Num to Int transition. With auto-coercion
results '-4 but -3.3' and '2 but 2.3' one could calculate a
return value '6 but 5.6' and maintain a maximum of information
while staying type safe.


Hmm, and floor(-0.0) == -1? But this does not carry over the
Num equality -0.0 == +0.0 to Int. So what is more important
sign or magnitude? BTW, does floor return an Int or a Num?

Regards, TSa.
--

Darren Duncan

unread,
Jan 30, 2007, 6:59:58 PM1/30/07
to perl6language
At 9:07 PM +0100 1/30/07, TSa wrote:
>BTW, does floor return an Int or a Num?

A floor() returns an Int of course, because by definition floor()
returns an integer. See http://en.wikipedia.org/wiki/Floor_function
for an explanation. Same with ceiling(), and some other operators.
If anything, people should be able to call floor() etc explicitly if
they want to make their Num->Int conversions explicit rather than
implicit. -- Darren Duncan

0 new messages