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

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

Thread Previous
From:
TSa
Date:
June 27, 2006 00:05
Subject:
Re: Can foo("123") dispatch to foo(Int) (was: Mutil Method Questions)
Message ID:
44A0D87D.2090801@orthogon.com
HaloO,

Paul Hodges wrote:
> so back to foo("bar"). What's the default behavior? String doesn't Num,
> does it? though is does convert if the value is good.... 

I think that Str and Num are disjoint mutually exclusive types.
If you want to get coercive behaviour you need an overloaded
&foo:(Str|Num) that handles conversions and redispatches.


> Does that mean foo("123") should or should not dispatch to foo(Int)?
> Or even foo(Num), for that matter.... Oy, I could see some headaches
> around setting these rules in mind, lol.... What's the DWIMmiest
> behavior hear?

I strongly believe that foo("123") should never *dispatch* to
&foo:(Int). The best in dwimmery would be an auto-generated
&foo:(Item) that handles coercions. Would that be switchable
by a 'use autocoercions' or so?


> I assume foo(1) would dispatch to foo(Int) rather than foo(Num), as the
> most restrictive fit, and I could see how that could blow up in my
> face. Is there some basic compile time warning (and a way to tell it
> "shut up, I know what I'm doing") that says "hey, these are
> suspiciously close signatures, are you sure this is what you wanted?"

Last time I checked the subtyping of Perl6 was set-theoretic. That means
to me that Int is a strict subset of Num along the lines of

   subset Int of Num where { .frac == 0 }

Note that Num might be implemented as a subclass of Int that adds the
fractional part! But subclassing is not subtyping.
-- 

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About