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

99 problems in Perl6: 32 and a question on number coercion

5 views
Skip to first unread message

Gabriele Renzi

unread,
Jan 2, 2007, 2:44:26 PM1/2/07
to perl6...@perl.org
Hi everyone!

I solved the (easy) problem 32, implementing gcd($a,$b).

You can check the code in the repository or on the web[1]
But while writing this I noticed that a function written as

sub gcd(Int $a, Int $b)

still accepts float/rational values in input.
I think I read once that a variable of type Int would accept these kind
of assignment but that the value would be coerced to the new type of the
variable, i.e.

my Int $a= 10.1 #=> 10

Could someone explain me what is the expected behaviour?

[1] http://sial.org/pbot/22088


--

blog en: http://www.riffraff.info
blog it: http://riffraff.blogsome.com
jabber : rff.rff at gmail dot com

Gabriele Renzi

unread,
Jan 3, 2007, 7:33:44 AM1/3/07
to perl6...@perl.org
Steffen Schwigon ha scritto:
> Hi!

>
> gabriele renzi <surren...@yahoo.it> writes:
>> Hi everyone!
>>
>> I solved the (easy) problem 32, implementing gcd($a,$b).
>> You can check the code in the repository or on the web[1]
>
> I looked at [1]. What's the purpose of "multi" in this case?
> (Maybe you wanted to write it as more than one subs, did you?)

look the comment:

# Yet, it should be possible to define it even for commutative rings
# other than Integers, so we use a multi sub.

I think that in these examples we should respect the old good
programmin' habits, including the "stay open for extension" idea.
If we'd use a normal sub we would limit all the future user of gcd
(which I expect to be legions ;) to using Int.

<snip>


>> Could someone explain me what is the expected behaviour?
>

> Last time I experimented with this, the type system in Pugs looked
> unfinished. Type constraints were syntactically accepted but worked
> similar to typeless code in Perl5. I'm not sure about the current
> state.

I see

> Anyway, in your example I hadn't expected a value coercion (from 10.1
> to 10), but something like an error if the type doesn't match.

this is what I'd like to see too, since I think it could help avoiding
casual errors, and the extensibility of the system is still safe because
of the multi things.
But I remember reading that, by default, perl6 will try to autocoerce
arguments.

Steffen Schwigon

unread,
Jan 3, 2007, 7:59:15 AM1/3/07
to perl6...@perl.org
gabriele renzi <surren...@yahoo.it> writes:
> Steffen Schwigon ha scritto:

>> I looked at [1]. What's the purpose of "multi" in this case?
>> (Maybe you wanted to write it as more than one subs, did you?)
>
> look the comment:
>
> # Yet, it should be possible to define it even for commutative rings
> # other than Integers, so we use a multi sub.

Damn, I ignored the comment as if it were my own. :-)

Steffen
--
Steffen Schwigon <schw...@webit.de>
Dresden Perl Mongers <http://dresden-pm.org/>
Deutscher Perl-Workshop <http://www.perl-workshop.de/>

0 new messages