Changeset 9955 for docs/Perl6/FAQ
- Timestamp:
- 04/15/06 13:35:33 (3 years ago)
- Files:
-
- 1 modified
-
docs/Perl6/FAQ/Capture.pod (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
docs/Perl6/FAQ/Capture.pod
r9953 r9955 1 1 = Q: What happened to references? 2 2 3 A: They have been superceded by Captures. 3 A: They have been superseded by Captures. 4 4 5 5 6 = Q: What is a Capture? … … 9 10 For example, the arguments in say("Hello", "World") is a Capture object 10 11 denoted by \("Hello", "World"). 12 11 13 12 14 = Q: Can I see how captures look like? Does \$x no longer work? … … 23 25 $xcap1 === $xcap2 === $xcap3; # true 24 26 27 25 28 = Q: What about capturing multiple arguments? 26 29 27 30 Here's how Captures look like deferred argument lists: 28 31 29 sub make_car (Str $model, Str $color = "black", Int $doors = 4);32 sub make_car (Str $model, Str $color? = "black", Int $doors = 4); 30 33 my $car_cap = \("Model T", doors => 2); # some positional, some named 31 34 … … 46 49 47 50 $cap[]; # postfix .[] 48 @$cap; # prefix @51 @$cap; # prefix @ 49 52 50 53 Both forms flatten under list context, so they may be used interchangeably. … … 79 82 80 83 $x === $(\$x); 81 84 85 86 = Q: What about multimethod dispatch and cases where there is more than one invocant? 87 88 There's always one invocant, but it may be an Array of several values (see below). 89 90 82 91 = Q: What happens when a named argument is repeated? 83 92 … … 90 99 board_ark( animal => "moose", animal => "elephant"); # 2 animals 91 100 92 A Capture object may hold named arguments that occur stwice or more. When it's101 A Capture object may hold named arguments that occur twice or more. When it's 93 102 bound to a variable in the Signature, if the sigil is "@", then it expands to a 94 103 list of all arguments (in the order they were specified). Otherwise (i.e. if … … 96 105 previous ones. 97 106 107 98 108 = Q: What do *$x, *@x and *%x mean? 99 109 100 110 The prefix * method casts an object into a Capture object, and merges it 101 into the currently constructing Capture(e.g. an argument list):111 into the Capture being currently constructed (e.g. an argument list): 102 112 103 113 my $cap = \(1, 2, x=>42); … … 111 121 my $p = (x => 42); f(*$p); # f(x => 42) 112 122 123 113 124 = Q: What does $x = \@y mean? 114 125 … … 121 132 instead of: 122 133 123 my @y2 := @$x; # $x contains no positional parts134 my @y2 := @$x; # WRONG: $x contains no positional parts 124 135 125 136 Manipulating the original value via the $x capture is still possible: … … 132 143 133 144 (Captures are immutable; their underlying data may not be.) 145 134 146 135 147 = Q: So $x = @y does not mean $x = \@y anymore. What does it mean then? … … 148 160 @y[][][]; 149 161 162 163 = Q: Does this mean I can't have something akin to a refrence to a reference? 164 165 [ hw !! 1 ] 166 You can, because a Signature can contain Captures. 167 168 sub apply (Code $f, Capture $args) { $f(*$args) } 169 170 150 171 = Q: How is @x = (1, 2, 3) different from @y := (1, 2, 3) ? 151 172 … … 158 179 However, these forms are valid: 159 180 160 *@y := (1,2,3); # slurpy @161 @y := ((1,2,3):); # list as invocant162 @y := ((1,2,3),); # list as first positional163 @y := (y => (1,2,3));# named binding181 *@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 164 185 165 186 But they are still different from the assignment form. 187 166 188 167 189 = Q: How is @x = (1, 2, 3) different from *@y := (1, 2, 3), then? … … 170 192 container itself, so this won't work: 171 193 172 @y.push(4); # error: cannot find method: List.push194 @y.push(4); # error: cannot find method: List.push 173 195 174 196 On the other hand, because variables are initialized by their sigils, so these 175 197 two mean the same: 176 198 177 my @x := []; # new Array object178 my @x; # implicitly does the same thing199 my @x := []; # new Array object 200 my @x; # implicitly does the same thing 179 201 180 202 so @x = (1, 2, 3) would simply populate the previously allocated Array object 181 203 with new elements. 204 182 205 183 206 = Q: Is there any difference between $x = (1, 2, 3) and @y = (1, 2, 3)? … … 191 214 @y.push(0); # works just fine 192 215 216 193 217 = Q: Is there any difference between $x = [1, 2, 3] and @y = [1, 2, 3]? 194 218 … … 206 230 @y.push(0); # works - @y.elems becomes 2 207 231 232 208 233 = Q: Is there any difference between $x := [1, 2, 3] and @y := [1, 2, 3]? 209 234
