# New Ticket Created by   
# Please include the string:  [perl #127598]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=127598 >



I was messing about with multiple dispatch subroutines.
And I ran into this little puzzler.



multi mysub($arg1) {
     say "mysub(one): $arg1";
}

multi mysub($arg1, $arg2) {
     say "mysub(two): '$arg1' then '$arg2'";
}



mysub(555);
mysub(123, 456);
mysub(999);
mysub (123, 456);  // subname <space> (parens)

The output looked like this:

mysub(one): 555
mysub(two): '123' then '456'
mysub(one): 999
mysub(one): 123 456        <= mysub multi was called with a single parameter, 
not two.


I fought this for an hour trying to figure out why it was calling the multi 
with only one parameter.
Eventually I gave up and emailed a friend who explained it was the space 
between the function name and the parenthesis that made the difference.
Apparently, the space causes the parens to be evaluated as a list context 
first, a single thing, and then that single thing is passed to the multi, so 
the multi that processes only one param is called.

Having majorly different behaviors be controlled by the presence or absence of 
a single white space is *terrible*. Especially in something that is already a 
complicated concept to debug (multiple dispatch).


HORRIBLY NONINTUITIVE.


Please consider that

     functionname \s* \(

should ALWAYS treat the first paren as attached to the function call, not as 
some indicator of list contextification or something else.

If I want to listcontextify the data first, then I would put TWO PAIRS of 
parens.

            functionname ((this, that));

Greg London

Reply via email to