--- Michael Lazzaro <[EMAIL PROTECTED]> wrote:
> On Tuesday, March 11, 2003, at 06:42 AM, Richard Proctor wrote:
> > If one has a simple sub such as factorial:
> >
> > sub factorial(int $a) {...}
> >
> > then one subsequently declares the multi form of factorial to pick
> up
> > the
> > non-integer form:
> >
> > multi factorial(num $a) {...}
> >
> > Does this promote the original declaration of factorial to a multi?
> > if not what happens?
>
> I would *strongly* suspect that it would fail, saying "can't
> redeclare
> 'factorial'" or something. The idea behind C<multi> is that if
> you're
> giving multiple possible signatures to a function, you have to do so
> *explicitly*. Otherwise, you might just be accidentally overriding
> some previous sub when you didn't really mean to -- and you'd
> _really_
> like to know when that was happening.
>
> sub foo($a,$b,$c) {...}
>
> ... lots of code inbetween ...
>
> multi foo(int $a) {...}
> multi foo(str $a) {...}
>
> You might have forgotten, when declaring the two C<multi>s, that --
> oops -- you had already used that subroutine name for something
> completely different! So you'd want it to tell you if you were
> redefining the C<foo> sub.
>
> So I'm betting it's an error. You have to go back and make the first
>
> one a C<multi>, if that's what you really meant to do.
>
> (Note that this means you can't give alternate signatures to
> functions
> that you've pulled in from a CPAN-style library, unless the library
> has
> given you permission to do so by making the functions C<multi> in the
>
> first place. Probably a good idea, on balance. You can do similar
> things with wrapper functions.)
But you can't wrap multi-ness, as far as I can tell.
[A6]
And it happens that the multimethod dispatch is smart enough to find
the ordinary single-invocant sysread method, even though it may not
have been explicitly declared a multimethod. Multimethod dispatch
happens to map directly onto ordinary method dispatch when there's only
one invocant.
At least, that's how it works this week...
[/A6]
To me, this suggests that multithods can be bolted on top of unithods.
And presumably, likewise multisubs can be bolted atop Plain Old Subs
(POSs).
So:
module CPANthing;
sub foo($x) {...}
module MyThing;
sub foo($x) {...} # ERROR - redefined: multi won't, but "my" will fix
sub foo(int $x) {...} # ERROR - must use multi
multi foo(int $x) {...} # OK
multi foo($x: @a) {...} # OK
multi foo($x: ?$y = 0) {...} # WARNING: compile-time or run-time?
=Austin