develooper Front page | perl.perl6.language | Postings from June 2006

Can foo("123") dispatch to foo(Int) (was: Mutil Method Questions)

Thread Next
Markus Laire
June 23, 2006 11:12
Can foo("123") dispatch to foo(Int) (was: Mutil Method Questions)
Message ID:
I'm sending this also to perl6-language, in case someone there knows
an answer to this.

On 6/23/06, Jonathan Scott Duff <> wrote:
> I don't think so. I think the "best candidate" prose is about
> choosing from types that have been specified, not autoconverting
> between types such that one of them will match the long name. In
> other words, when you have
>     multi sub foo (Num) { ... }
>     multi sub foo (Int) { ... }
>     foo(1);
>     foo("123");
>     foo("bar");
> foo(Int) is the best candidate for the first one because 1 is an Int.
> But in the second and third calls, there is no "best candidate"
> because strings were passed.  Would you expect the third call to
> succeed because "bar" can be converted into the number 0?

I think you are right, but I don't see that mentioned anywhere in Synopses.

S12 clearly says that for a particular short name ("foo" here) *all*
visible long names ("foo(Num)" and "foo(Int)" here) are candidates and
best candidate *is* called (no matter how bad it is) -- unless there's
a tie.

So that would seem to say that for foo("123") above foo(Int) would be
called because it's the best candidate.

Which one is "better" for foo("bar")? foo(Int) or foo(Num)? Or is that a tie?

And what about other types?
e.g. if String can't ever be "best candidate" for Int, then does that
mean that neither can Int ever be "best candidate" for Num, because
they are different types?

> The programmer put type information in the sig for a reason. I think
> that reason is that they wanted to be careful about what was allowed
> to be passed to the subroutine.  Autoconversion seems to defeat that.
> -Scott
> --
> Jonathan Scott Duff

Markus Laire

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About