Thanks to all the feedback to yesterday's Capture.pod post, today I updated Capture.pod with more examples, including a more elaborate description on single/multiple vs subroutine/method dispatch (as requested by Humberrto), which I reproduce below:
What about multi-dispatch?
Multi-dispatch governs cases where multiple subroutines or methods share the same name, relying on the arity and types of tie-breaking parameters in their parameter list (Signature):
multi f ($x: $z) { say 'A' }
multi f (Int $x, Int $y: $z) { say 'B' }
multi f (Str $x, Str $y: $z) { say 'C' }
f(1, 2); # goes to A
f(1, 2, 3); # goes to B
f('x', 'y', 'z'); # goes to C
During multi-dispatch, a tie-breaking parameter may bind to the invocant argument (e.g. for multi-methods), or one of the positional arguments.
However, regardless of single- or multi- dispatch, the argument list (Capture) can never have more than one invocants. Typically, the presence of an invocant indicates a method-call (which may fall back to a subroutine-call); the lack of invocant means a subroutine-call.
1.foo(2); # Int.foo with 1 as "self"; if not found,
# then fall back to foo(1, 2)
foo(1: 2); # same as above
bar(1, 2); # never looks at Int.bar;
# calls &bar in lexical/package scope
Method/subroutine calls are determined by the presence of an invocant at the calling site. Single/multi dispatch are determined by the presence of multi in the declaration site. The two concepts are entirely orthogonal.
The last line was key -- it took me several months to realize that in the Synopses, the so-called "invocant parameters" in multisubs really have nothing to do to with "invocant arguments" in method calls. Hence I've started referring to them as "tiebreaking parameters" to reduce general confusion.
Also, instead of mixing multi and method with the MMD acronym, I've taken to say sub/method calls and single/multi dispatch, to further clarify that they are distinct (and quite unrelated) concepts. Hope that helps...
I'll have less cycles to stay on #perl6 the next four days, as all three projects at $job are drawing into their closure, and I'd like to deliver them properly before going back to Pugs. However, we are just 35 commits away from r10000, and it might make a nice birthday gift to myself... So we'll see if I manage to send in some commits here and there. :-)
THANK YOU very much for posting all of this stuff. I am constantly amazed by how much code you produce. And am astounded by how well you explain and document it... and then am flabbergasted when I realize you have three jobs as well.
-ben
Posted by: Ben Bennett | 2006.04.17 at 02:03 AM
Minor correction: it's quite true that bar(1, 2) never looks at Int.foo, but I suspect the fact that it never looks at Int.bar is rather more interesting.
Posted by: Smylers | 2006.04.17 at 06:35 AM
YOU ABSOLUTELY ROCK. This is a knot in my head since Larry introduced the old "multi method" syntax in p6. Thanks.
Posted by: Humberto Massa | 2006.04.17 at 08:19 PM
Smyler: Thanks, fixed. :)
Posted by: Audrey T | 2006.04.17 at 08:59 PM
you're so wonderful!
Posted by: Clark Kuo | 2006.04.18 at 12:09 AM