Changeset 9961 for docs/Perl6/FAQ
- Timestamp:
- 04/15/06 18:05:49 (3 years ago)
- Files:
-
- 1 modified
-
docs/Perl6/FAQ/Capture.pod (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
docs/Perl6/FAQ/Capture.pod
r9960 r9961 8 8 bindings and function calls. 9 9 10 =head2 What happened to references? 10 =head2 What 11 12 =head3 What happened to references? 11 13 12 14 They have been superseded by Capture objects. 13 15 14 =head 2What is a Capture?16 =head3 What is a Capture? 15 17 16 18 A Capture is an object representing arguments in a function call. 17 19 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? 20 For example, the arguments in C<say("Hello", "World")> is a Capture 21 object denoted by C<\("Hello", "World")>. 22 23 =head2 How 24 25 =head3 Can I see how captures look like? Does \$x no longer work? 22 26 23 27 Prefix C<\> is now the Capture constructor. These are all equivalent: … … 27 31 my $xcap3 = \($x :); 28 32 29 All three represent an argument list with a single invocant C<$x>, and hence are30 a ll equivalent with each other:33 All three represent an argument list with a single invocant C<$x>, 34 and hence are all equivalent with each other: 31 35 32 36 $xcap1 === $xcap2 === $xcap3; # true 33 37 34 =head 2What about capturing multiple arguments?38 =head3 What about capturing multiple arguments? 35 39 36 40 Here's how Captures look like deferred argument lists: … … 54 58 say "It has $car_cap<doors> doors" # 2 doors 55 59 56 =head 2How 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 62 As you can see above, a Capture object holds both positional and named 63 parts. If you want to retrieve all positional parts, there are two ways: 60 64 61 65 $cap[]; # postfix .[] 62 66 @$cap; # prefix @ 63 67 64 Both forms flatten under list context, so they may be used interchangeably. 65 However, only the postfix C<.[]> form interpolates in a string. 68 Both forms flatten under list context, so they may be used 69 interchangeably. However, only the postfix C<.[]> form interpolates in 70 a string. 66 71 67 72 Similarly, access to named arguments is available by treating the Capture … … 73 78 Note that the positional parts do not include the invocant. 74 79 75 =head 2What do you mean by "invocant"?76 77 When you call a object's method, the C<self> inside the method is set to that78 object. These are all equivalent:80 =head3 What do you mean by "invocant"? 81 82 When you call a object's method, the C<self> inside the method is set 83 to that object. These are all equivalent: 79 84 80 85 $fh.say("Hi!"); … … 82 87 say($fh: "Hi!"); 83 88 84 Note that an argument list can have at most one invocant. You can construct a85 Capture object with an invocant using the same syntax:89 Note that an argument list can have at most one invocant. You can 90 construct a Capture object with an invocant using the same syntax: 86 91 87 92 my $cap = \($fh: "Hi!"); … … 95 100 $x === $(\$x); 96 101 97 =head 2What 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? 98 103 99 104 In the argument list (Capture), there is always zero or one invocant I<argument>. 100 105 101 Multimethod dispatch governs cases where multiple invocant I<parameters> are102 declared in the parameter list (Signature); each of them may bind to the invocant 103 argument, or one of the positional arguments.104 105 =head 2What happens when a named argument is repeated?106 Multimethod dispatch governs cases where multiple invocant I<parameters> 107 are declared in the parameter list (Signature); each of them may bind 108 to the invocant argument, or one of the positional arguments. 109 110 =head3 What happens when a named argument is repeated? 106 111 107 112 Let's look at some examples: … … 113 118 board_ark( animal => "moose", animal => "elephant"); # 2 animals 114 119 115 A Capture object may hold named arguments that occur twice or more. When it's116 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 argument120 overrides the previous ones.121 122 =head 2What do *$x, *@x and *%x mean?123 124 The prefix C<*> method casts an object into a Capture object, and merges it125 i nto the Capture currently being constructed (e.g. an argument list):120 A Capture object may hold named arguments that occur twice or more. 121 When it's bound to a variable in the Signature, if the sigil is C<@>, then 122 it expands to a list of all arguments (in the order they were specified). 123 124 Otherwise (i.e. if it's bound to a scalar or a slurpy hash), the last 125 argument overrides the previous ones. 126 127 =head3 What do *$x, *@x and *%x mean? 128 129 The prefix C<*> method casts an object into a Capture object, and merges 130 it into the Capture currently being constructed (e.g. an argument list): 126 131 127 132 my $cap = \(1, 2, x=>42); 128 133 f(*$cap); # f(1, 2, x=>42) 129 134 130 Array, List, Hash and Pair objects cast into Capture objects in obvious ways: 135 Array, List, Hash and Pair objects cast into Capture objects in obvious 136 ways: 131 137 132 138 my $a = [1, 2]; f(*$a); # f(1, 2) … … 135 141 my $p = (x => 42); f(*$p); # f(x => 42) 136 142 137 =head 2What does $x = \@y mean?143 =head3 What does $x = \@y mean? 138 144 139 145 The same as C<$x = (@y: )>. That is, a Capture of one array as invocant. … … 150 156 151 157 $$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 159 Note the need for the extra $ sigil, implying we are accessing the 160 captured invocant. C<@$x.push()> would mean an attempt to add an extra 161 positional argument into C<$x>; this would fail as all parts are immutable 162 in an Capture object. 156 163 157 164 (Captures are immutable; their underlying data may not be.) 158 165 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 168 It means assigning C<$x> with the object bound to C<@y> (typically an 169 Array object). 162 170 163 171 This does not create Capture objects; to get back C<@y>, C<@$x> would do. … … 172 180 @y[][][]; 173 181 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 184 You can, as a Capture can contain another capture object in its invocant 185 slot: 177 186 178 187 my $x = \\3; 179 188 say $$$x; # same as "say 3" 180 189 181 =head 2Can I create a "pass-through" function that captures all arguments?190 =head3 Can I create a "pass-through" function that captures all arguments? 182 191 183 192 Yes, using the C<\$> signature: … … 188 197 The C<$args> above becomes a Capture object. 189 198 190 =head 2How 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) ? 191 200 192 201 The latter is an error. :-) 193 202 194 The C<:=> operator binds its left hand side (a Signature object) to its right195 hand side (a Capture object), so the latter form is akin to:203 The C<:=> operator binds its left hand side (a Signature object) to its 204 right hand side (a Capture object), so the latter form is akin to: 196 205 197 206 sub foo (@y is rw) { ... } … … 207 216 But they are still different from the assignment form. 208 217 209 =head 2How 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 the212 container itself, so this won't work:218 =head3 How is @x = (1, 2, 3) different from *@y := (1, 2, 3), then? 219 220 This means that while C<@y> holds the values 1, 2, and 3, you cannot 221 modify the container itself, so this won't work: 213 222 214 223 @y.push(4); # error: cannot find method: List.push 215 224 216 On the other hand, because variables are initialized by their sigils, so these217 two mean the same:225 On the other hand, because variables are initialized by their sigils, 226 so these two mean the same: 218 227 219 228 my @x := []; # new Array object 220 229 my @x; # implicitly does the same thing 221 230 222 so C<@x = (1, 2, 3)> would simply populate the previously allocated Array object223 with new elements.224 225 =head 2Is 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 with228 the list-associative C<< infix:<,> >> operator, but it is flattened in the second229 case, and its elements are put into the previously allocated Array container 230 bound to C<@y>:231 so C<@x = (1, 2, 3)> would simply populate the previously allocated 232 Array object with new elements. 233 234 =head3 Is there any difference between $x = (1, 2, 3) and @y = (1, 2, 3)? 235 236 Yes. The right-hand side in both case is a single List object constructed 237 with the list-associative C<< infix:<,> >> operator, but it is flattened 238 in the second case, and its elements are put into the previously allocated 239 Array container bound to C<@y>: 231 240 232 241 $x.push(0); # error: cannot find method: List.push 233 242 @y.push(0); # works just fine 234 243 235 =head 2Is 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 flatten238 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 246 Yes. As in Perl 5, the Array constructor C<circumfix:[ ]> does not 247 flatten under list context, so C<@y> receives a List with one element 248 (an Array object), which then becomes C<@y[0]>: 240 249 241 250 $x.elems; # 3 … … 248 257 @y.push(0); # works - @y.elems becomes 2 249 258 250 =head 2Is 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]? 251 260 252 261 For the usual method-based operations, they are pretty much interchangeable: … … 260 269 @y = 42; # works - @y.elems is now 1 261 270 262 Note that C<$x = 42> fails because the C<:=> in C<$x := [1, 2, 3]> changes the263 underlying container of $x from a Scalar into an Array. Compare this with the 264 assignment case:271 Note that C<$x = 42> fails because the C<:=> in C<$x := [1, 2, 3]> 272 changes the underlying container of $x from a Scalar into an Array. 273 Compare this with the assignment case: 265 274 266 275 $x = [1,2,3]; … … 272 281 $x = 42; # fails - Int doesn't handle scalar assignment either 273 282 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
