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.)

MikeL



Reply via email to