Changeset 9955 for docs/Perl6/FAQ

Show
Ignore:
Timestamp:
04/15/06 13:35:33 (3 years ago)
Author:
gaal
Message:

r9995@sike: roo | 2006-04-15 14:34:32 +0300

  • Capture.pod - minor edits and a new question about ref-to-ref
Files:
1 modified

Legend:

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

    r9953 r9955  
    11= Q: What happened to references? 
    22 
    3 A: They have been superceded by Captures. 
     3A: They have been superseded by Captures. 
     4 
    45 
    56= Q: What is a Capture? 
     
    910For example, the arguments in say("Hello", "World") is a Capture object 
    1011denoted by \("Hello", "World"). 
     12 
    1113 
    1214= Q: Can I see how captures look like? Does \$x no longer work? 
     
    2325  $xcap1 === $xcap2 === $xcap3; # true 
    2426 
     27 
    2528= Q: What about capturing multiple arguments? 
    2629 
    2730Here's how Captures look like deferred argument lists: 
    2831 
    29   sub make_car (Str $model, Str $color = "black", Int $doors = 4); 
     32  sub make_car (Str $model, Str $color? = "black", Int $doors = 4); 
    3033  my $car_cap = \("Model T", doors => 2); # some positional, some named 
    3134 
     
    4649 
    4750  $cap[];  # postfix .[] 
    48   @$cap; # prefix @ 
     51  @$cap;   # prefix @ 
    4952 
    5053Both forms flatten under list context, so they may be used interchangeably. 
     
    7982 
    8083  $x === $(\$x); 
    81    
     84 
     85 
     86= Q: What about multimethod dispatch and cases where there is more than one invocant? 
     87 
     88There's always one invocant, but it may be an Array of several values (see below). 
     89 
     90 
    8291= Q: What happens when a named argument is repeated? 
    8392 
     
    9099  board_ark( animal => "moose", animal => "elephant"); # 2 animals  
    91100 
    92 A Capture object may hold named arguments that occurs twice or more.  When it's 
     101A Capture object may hold named arguments that occur twice or more.  When it's 
    93102bound to a variable in the Signature, if the sigil is "@", then it expands to a 
    94103list of all arguments (in the order they were specified).  Otherwise (i.e. if 
     
    96105previous ones. 
    97106 
     107 
    98108= Q: What do *$x, *@x and *%x mean? 
    99109 
    100110The prefix * method casts an object into a Capture object, and merges it 
    101 into the currently constructing Capture (e.g. an argument list): 
     111into the Capture being currently constructed (e.g. an argument list): 
    102112 
    103113    my $cap = \(1, 2, x=>42); 
     
    111121    my $p = (x => 42);  f(*$p);     # f(x => 42) 
    112122 
     123 
    113124= Q: What does $x = \@y mean? 
    114125 
     
    121132instead of: 
    122133 
    123   my @y2 := @$x; # $x contains no positional parts 
     134  my @y2 := @$x; # WRONG: $x contains no positional parts 
    124135 
    125136Manipulating the original value via the $x capture is still possible: 
     
    132143 
    133144(Captures are immutable; their underlying data may not be.) 
     145 
    134146 
    135147= Q: So $x = @y does not mean $x = \@y anymore. What does it mean then? 
     
    148160  @y[][][]; 
    149161 
     162 
     163= Q: Does this mean I can't have something akin to a refrence to a reference? 
     164 
     165[ hw !! 1 ] 
     166You can, because a Signature can contain Captures. 
     167 
     168  sub apply (Code $f, Capture $args) { $f(*$args) } 
     169 
     170 
    150171= Q: How is @x = (1, 2, 3) different from @y := (1, 2, 3) ? 
    151172 
     
    158179However, these forms are valid: 
    159180 
    160   *@y := (1,2,3);       # slurpy @ 
    161   @y := ((1,2,3):);      # list as invocant 
    162   @y := ((1,2,3),);      # list as first positional 
    163   @y := (y => (1,2,3)); # named binding 
     181  *@y := (1,2,3);         # slurpy @ 
     182  @y  := ((1,2,3):);      # list as invocant 
     183  @y  := ((1,2,3),);      # list as first positional 
     184  @y  := (y => (1,2,3)); # named binding 
    164185 
    165186But they are still different from the assignment form. 
     187 
    166188 
    167189= Q: How is @x = (1, 2, 3) different from *@y := (1, 2, 3), then? 
     
    170192container itself, so this won't work: 
    171193 
    172   @y.push(4); # error: cannot find method: List.push 
     194  @y.push(4);   # error: cannot find method: List.push 
    173195 
    174196On the other hand, because variables are initialized by their sigils, so these 
    175197two mean the same: 
    176198 
    177   my @x := []; # new Array object  
    178   my @x;      # implicitly does the same thing 
     199  my @x := [];  # new Array object  
     200  my @x;        # implicitly does the same thing 
    179201 
    180202so @x = (1, 2, 3) would simply populate the previously allocated Array object 
    181203with new elements. 
     204 
    182205 
    183206= Q: Is there any difference between $x = (1, 2, 3) and @y = (1, 2, 3)? 
     
    191214    @y.push(0); # works just fine 
    192215 
     216 
    193217= Q: Is there any difference between $x = [1, 2, 3] and @y = [1, 2, 3]? 
    194218 
     
    206230    @y.push(0);     # works - @y.elems becomes 2  
    207231 
     232 
    208233= Q: Is there any difference between $x := [1, 2, 3] and @y := [1, 2, 3]? 
    209234