Changeset 9965 for docs/Perl6/FAQ

Show
Ignore:
Timestamp:
04/16/06 18:30:19 (3 years ago)
Author:
audreyt
Message:

* Capture.pod: expands the examples; revamped the section on single/multi dispatch versus method/sub calls

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • docs/Perl6/FAQ/Capture.pod

    r9964 r9965  
    102102  $x === $(\$x); 
    103103 
    104 =head3 What about multimethod dispatch and cases where there is more than one invocant? 
    105  
    106 In the argument list (Capture), there is always zero or one invocant I<argument>. 
    107  
    108 Multimethod dispatch governs cases where multiple invocant I<parameters> 
    109 are declared in the parameter list (Signature); each of them may bind 
    110 to the invocant argument, or one of the positional arguments. 
     104=head3 What about multi-dispatch? 
     105 
     106Multi-dispatch governs cases where multiple subroutines or methods share 
     107the same name, relying on the arity and types of tie-breaking I<parameters> 
     108in their parameter list (Signature): 
     109 
     110    multi f ($x: $z)             { say 'A' } 
     111    multi f (Int $x, Int $y: $z) { say 'B' } 
     112    multi f (Str $x, Str $y: $z) { say 'C' } 
     113 
     114    f(1, 2);            # goes to A 
     115    f(1, 2, 3);         # goes to B 
     116    f('x', 'y', 'z');   # goes to C 
     117 
     118During multi-dispatch, a tie-breaking parameter may bind to the invocant 
     119argument (e.g. for multi-methods), or one of the positional arguments. 
     120 
     121However, regardless of single- or multi- dispatch, the argument list (Capture) 
     122can never have more than one invocants.  Typically, the presence of an invocant 
     123indicates a method-call (which may fall back to a subroutine-call); the lack of 
     124invocant means a subroutine-call. 
     125 
     126    1.foo(2);  # Int.foo with 1 as "self"; if not found, fallback to foo(1, 2) 
     127    foo(1: 2); # same as above 
     128 
     129    bar(1, 2); # never looks at Int.foo; calls &foo in lexical/package scope 
     130 
     131Method/subroutine calls are determined by the presence of an invocant at the 
     132calling site.  Single/multi dispatch are determined by the precense of "multi" 
     133in the declaration site.  The two concepts are entirely orthogonal. 
    111134 
    112135=head3 What happens when a named argument is repeated? 
     
    151174    my $p = (x => 42);  f(*$p);     # f(x => 42) 
    152175 
     176It also works with the C<@> and C<%> sigils: 
     177 
     178    my @a = (1, 2);     f(*@a);     # f(1, 2) 
     179    my @a := [1, 2];    f(*@a);     # f(1, 2) 
     180    my %h = (x => 42);  f(*%a);     # f(x => 42) 
     181    my %h := {x => 42}; f(*%h);     # f(x => 42) 
     182 
     183 
    153184=head3 What does $x = \@y mean? 
    154185