Skip to content

Commit c047f87

Browse files
committed
Correction for @ coercer
- It does more than just remove the Scalar - Show <> decont methodop - Show a bug where using `@` instead of `<>` leaks memory
1 parent 2ed89df commit c047f87

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

doc/Language/containers.pod6

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,20 @@ As hinted above, scalar containers prevent that flattening:
283283
my @a = 1, 2, 3;
284284
say f $@a, 4, 5; # OUTPUT: «3␤»
285285
286-
The C<@> character can also be used as a prefix to remove a scalar container:
286+
The C<@> character can also be used as a prefix to coerce the argument to a list, thus removing a scalar container:
287287
288288
my $x = (1, 2, 3);
289289
.say for @$x; # 3 iterations
290290
291+
However, the C«<>» is more appropriate to decontainerize items that aren't
292+
lists:
293+
294+
my $x = ^Inf .grep: *.is-prime;
295+
say "$_ is prime" for @$x; # WRONG! List keeps values, thus leaking memory
296+
say "$_ is prime" for $x<>; # RIGHT. Simply decont the Seq
297+
298+
my $y := ^Inf .grep: *.is-prime; # Even better; no Scalars involved at all
299+
291300
Methods generally don't care whether their invocant is in a scalar, so
292301
293302
my $x = (1, 2, 3);

0 commit comments

Comments
 (0)