Show
Ignore:
Timestamp:
04/15/06 18:05:49 (3 years ago)
Author:
gaal
Message:

r10003@sike: roo | 2006-04-15 19:02:21 +0300

  • FAQ::Capture - added a section on motivation, and [minor] reflowed long lines.
Files:
1 modified

Legend:

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

    r9960 r9961  
    88bindings and function calls. 
    99 
    10 =head2 What happened to references? 
     10=head2 What 
     11 
     12=head3 What happened to references? 
    1113 
    1214They have been superseded by Capture objects. 
    1315 
    14 =head2 What is a Capture? 
     16=head3 What is a Capture? 
    1517 
    1618A Capture is an object representing arguments in a function call. 
    1719 
    18 For example, the arguments in C<say("Hello", "World")> is a Capture object 
    19 denoted by C<\("Hello", "World")>. 
    20  
    21 =head2 Can I see how captures look like? Does \$x no longer work? 
     20For example, the arguments in C<say("Hello", "World")> is a Capture 
     21object denoted by C<\("Hello", "World")>. 
     22 
     23=head2 How 
     24 
     25=head3 Can I see how captures look like? Does \$x no longer work? 
    2226 
    2327Prefix C<\> is now the Capture constructor.  These are all equivalent: 
     
    2731  my $xcap3 = \($x :); 
    2832 
    29 All three represent an argument list with a single invocant C<$x>, and hence are 
    30 all equivalent with each other: 
     33All three represent an argument list with a single invocant C<$x>, 
     34and hence are all equivalent with each other: 
    3135 
    3236  $xcap1 === $xcap2 === $xcap3; # true 
    3337 
    34 =head2 What about capturing multiple arguments? 
     38=head3 What about capturing multiple arguments? 
    3539 
    3640Here's how Captures look like deferred argument lists: 
     
    5458  say "It has $car_cap<doors> doors" # 2 doors 
    5559 
    56 =head2 How do I extract all positional arguments or all named arguments? 
    57  
    58 As you can see above, a Capture object holds both positional and named parts. 
    59 If you want to retrieve all positional parts, there are two ways: 
     60=head3 How do I extract all positional arguments or all named arguments? 
     61 
     62As you can see above, a Capture object holds both positional and named 
     63parts.  If you want to retrieve all positional parts, there are two ways: 
    6064 
    6165  $cap[];  # postfix .[] 
    6266  @$cap;   # prefix @ 
    6367 
    64 Both forms flatten under list context, so they may be used interchangeably. 
    65 However, only the postfix C<.[]> form interpolates in a string. 
     68Both forms flatten under list context, so they may be used 
     69interchangeably.  However, only the postfix C<.[]> form interpolates in 
     70a string. 
    6671 
    6772Similarly, access to named arguments is available by treating the Capture 
     
    7378Note that the positional parts do not include the invocant. 
    7479 
    75 =head2 What do you mean by "invocant"? 
    76  
    77 When you call a object's method, the C<self> inside the method is set to that 
    78 object. These are all equivalent: 
     80=head3 What do you mean by "invocant"? 
     81 
     82When you call a object's method, the C<self> inside the method is set 
     83to that object. These are all equivalent: 
    7984 
    8085  $fh.say("Hi!"); 
     
    8287  say($fh: "Hi!"); 
    8388 
    84 Note that an argument list can have at most one invocant. You can construct a 
    85 Capture object with an invocant using the same syntax: 
     89Note that an argument list can have at most one invocant. You can 
     90construct a Capture object with an invocant using the same syntax: 
    8691 
    8792  my $cap = \($fh: "Hi!"); 
     
    95100  $x === $(\$x); 
    96101 
    97 =head2 What about multimethod dispatch and cases where there is more than one invocant? 
     102=head3 What about multimethod dispatch and cases where there is more than one invocant? 
    98103 
    99104In the argument list (Capture), there is always zero or one invocant I<argument>. 
    100105 
    101 Multimethod dispatch governs cases where multiple invocant I<parameters> are 
    102 declared in the parameter list (Signature); each of them may bind to the invocant 
    103 argument, or one of the positional arguments. 
    104  
    105 =head2 What happens when a named argument is repeated? 
     106Multimethod dispatch governs cases where multiple invocant I<parameters> 
     107are declared in the parameter list (Signature); each of them may bind 
     108to the invocant argument, or one of the positional arguments. 
     109 
     110=head3 What happens when a named argument is repeated? 
    106111 
    107112Let's look at some examples: 
     
    113118  board_ark( animal => "moose", animal => "elephant"); # 2 animals  
    114119 
    115 A Capture object may hold named arguments that occur twice or more.  When it's 
    116 bound to a variable in the Signature, if the sigil is C<@>, then it expands to a 
    117 list of all arguments (in the order they were specified). 
    118  
    119 Otherwise (i.e. if it's bound to a scalar or a slurpy hash), the last argument 
    120 overrides the previous ones. 
    121  
    122 =head2 What do *$x, *@x and *%x mean? 
    123  
    124 The prefix C<*> method casts an object into a Capture object, and merges it 
    125 into the Capture currently being constructed (e.g. an argument list): 
     120A Capture object may hold named arguments that occur twice or more. 
     121When it's bound to a variable in the Signature, if the sigil is C<@>, then 
     122it expands to a list of all arguments (in the order they were specified). 
     123 
     124Otherwise (i.e. if it's bound to a scalar or a slurpy hash), the last 
     125argument overrides the previous ones. 
     126 
     127=head3 What do *$x, *@x and *%x mean? 
     128 
     129The prefix C<*> method casts an object into a Capture object, and merges 
     130it into the Capture currently being constructed (e.g. an argument list): 
    126131 
    127132    my $cap = \(1, 2, x=>42); 
    128133    f(*$cap);   # f(1, 2, x=>42) 
    129134 
    130 Array, List, Hash and Pair objects cast into Capture objects in obvious ways: 
     135Array, List, Hash and Pair objects cast into Capture objects in obvious 
     136ways: 
    131137 
    132138    my $a = [1, 2];     f(*$a);     # f(1, 2) 
     
    135141    my $p = (x => 42);  f(*$p);     # f(x => 42) 
    136142 
    137 =head2 What does $x = \@y mean? 
     143=head3 What does $x = \@y mean? 
    138144 
    139145The same as C<$x = (@y: )>. That is, a Capture of one array as invocant.  
     
    150156 
    151157  $$x.push("another element"); 
    152    
    153 Note the need for the extra $ sigil, implying we are accessing the captured invocant. 
    154 C<@$x.push()> would mean an attempt to add an extra positional argument into 
    155 C<$x>; this would fail as all parts are immutable in an Capture object. 
     158 
     159Note the need for the extra $ sigil, implying we are accessing the 
     160captured invocant.  C<@$x.push()> would mean an attempt to add an extra 
     161positional argument into C<$x>; this would fail as all parts are immutable 
     162in an Capture object. 
    156163 
    157164(Captures are immutable; their underlying data may not be.) 
    158165 
    159 =head2 So $x = @y does not mean $x = \@y anymore. What does it mean then? 
    160  
    161 It means assigning C<$x> with the object bound to C<@y> (typically an Array object). 
     166=head3 So $x = @y does not mean $x = \@y anymore. What does it mean then? 
     167 
     168It means assigning C<$x> with the object bound to C<@y> (typically an 
     169Array object). 
    162170 
    163171This does not create Capture objects; to get back C<@y>, C<@$x> would do. 
     
    172180  @y[][][]; 
    173181 
    174 =head2 Does this mean I can't have something akin to a reference to a reference? 
    175  
    176 You can, as a Capture can contain another capture object in its invocant slot: 
     182=head3 Does this mean I can't have something akin to a reference to a reference? 
     183 
     184You can, as a Capture can contain another capture object in its invocant 
     185slot: 
    177186 
    178187    my $x = \\3; 
    179188    say $$$x; # same as "say 3" 
    180189 
    181 =head2 Can I create a "pass-through" function that captures all arguments? 
     190=head3 Can I create a "pass-through" function that captures all arguments? 
    182191 
    183192Yes, using the C<\$> signature: 
     
    188197The C<$args> above becomes a Capture object. 
    189198 
    190 =head2 How is @x = (1, 2, 3) different from @y := (1, 2, 3) ? 
     199=head3 How is @x = (1, 2, 3) different from @y := (1, 2, 3) ? 
    191200 
    192201The latter is an error. :-) 
    193202 
    194 The C<:=> operator binds its left hand side (a Signature object) to its right 
    195 hand side (a Capture object), so the latter form is akin to: 
     203The C<:=> operator binds its left hand side (a Signature object) to its 
     204right hand side (a Capture object), so the latter form is akin to: 
    196205 
    197206  sub foo (@y is rw) { ... } 
     
    207216But they are still different from the assignment form. 
    208217 
    209 =head2 How is @x = (1, 2, 3) different from *@y := (1, 2, 3), then? 
    210  
    211 This means that while C<@y> holds the values 1, 2, and 3, you cannot modify the 
    212 container itself, so this won't work: 
     218=head3 How is @x = (1, 2, 3) different from *@y := (1, 2, 3), then? 
     219 
     220This means that while C<@y> holds the values 1, 2, and 3, you cannot 
     221modify the container itself, so this won't work: 
    213222 
    214223  @y.push(4);   # error: cannot find method: List.push 
    215224 
    216 On the other hand, because variables are initialized by their sigils, so these 
    217 two mean the same: 
     225On the other hand, because variables are initialized by their sigils, 
     226so these two mean the same: 
    218227 
    219228  my @x := [];  # new Array object  
    220229  my @x;        # implicitly does the same thing 
    221230 
    222 so C<@x = (1, 2, 3)> would simply populate the previously allocated Array object 
    223 with new elements. 
    224  
    225 =head2 Is there any difference between $x = (1, 2, 3) and @y = (1, 2, 3)? 
    226  
    227 Yes.  The right-hand side in both case is a single List object constructed with 
    228 the list-associative C<< infix:<,> >> operator, but it is flattened in the second 
    229 case, and its elements are put into the previously allocated Array container 
    230 bound to C<@y>: 
     231so C<@x = (1, 2, 3)> would simply populate the previously allocated 
     232Array object with new elements. 
     233 
     234=head3 Is there any difference between $x = (1, 2, 3) and @y = (1, 2, 3)? 
     235 
     236Yes.  The right-hand side in both case is a single List object constructed 
     237with the list-associative C<< infix:<,> >> operator, but it is flattened 
     238in the second case, and its elements are put into the previously allocated 
     239Array container bound to C<@y>: 
    231240 
    232241    $x.push(0); # error: cannot find method: List.push 
    233242    @y.push(0); # works just fine 
    234243 
    235 =head2 Is there any difference between $x = [1, 2, 3] and @y = [1, 2, 3]? 
    236  
    237 Yes.  As in Perl 5, the Array constructor C<circumfix:[ ]> does not flatten 
    238 under list context, so C<@y> receives a List with one element (an Array 
    239 object), which then becomes C<@y[0]>: 
     244=head3 Is there any difference between $x = [1, 2, 3] and @y = [1, 2, 3]? 
     245 
     246Yes.  As in Perl 5, the Array constructor C<circumfix:[ ]> does not 
     247flatten under list context, so C<@y> receives a List with one element 
     248(an Array object), which then becomes C<@y[0]>: 
    240249 
    241250    $x.elems; # 3 
     
    248257    @y.push(0);     # works - @y.elems becomes 2  
    249258 
    250 =head2 Is there any difference between $x := [1, 2, 3] and @y := [1, 2, 3]? 
     259=head3 Is there any difference between $x := [1, 2, 3] and @y := [1, 2, 3]? 
    251260 
    252261For the usual method-based operations, they are pretty much interchangeable: 
     
    260269    @y = 42; # works - @y.elems is now 1 
    261270 
    262 Note that C<$x = 42> fails because the C<:=> in C<$x := [1, 2, 3]> changes the 
    263 underlying container of $x from a Scalar into an Array.  Compare this with the 
    264 assignment case: 
     271Note that C<$x = 42> fails because the C<:=> in C<$x := [1, 2, 3]> 
     272changes the underlying container of $x from a Scalar into an Array. 
     273Compare this with the assignment case: 
    265274 
    266275    $x = [1,2,3]; 
     
    272281    $x = 42;        # fails - Int doesn't handle scalar assignment either 
    273282 
     283=head2 Why 
     284 
     285=head3 What prompted this change? Was there something innately lacking in refrences? 
     286 
     287[needs beefing up.] 
     288 
     289* references lose form and type 
     290 
     291* Capture also does most of what globs did, but in a safer and saner manner 
     292 
     293* the concept of Capture is applicable in match results