File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -597,6 +597,33 @@ assign them to an array:
597
597
my @a = do for 1, 2, 3 { $_ * 2 }; @a.say; # OUTPUT: «[2 4 6]»
598
598
my @b = (for 1, 2, 3 { $_ * 2 }); @b.say; # OUTPUT: «[2 4 6]»
599
599
600
+ This implies that, if the results of the loop are not assigned, they will be
601
+ in a L < sink context|/language/contexts#index-entry-sink_context > :
602
+
603
+ = for code
604
+ class Sunk {
605
+ has $.titanic;
606
+ method sink {
607
+ say "Sinking $!titanic";
608
+ }
609
+ }
610
+ Sunk.new( :titanic($_) ) for ^3;
611
+ for 1 {
612
+ say "About to sink";
613
+ Sunk.new( :titanic($_) );
614
+ }
615
+ # OUTPUT:
616
+ # Sinking 0
617
+ # Sinking 1
618
+ # Sinking 2
619
+ # About to sink
620
+ # Sinking 1
621
+
622
+
623
+ The first loop creates three elements but they are in a sink context, so its
624
+ C < sink > method is called. In the second loop, its last statement will be in a
625
+ sink context, so it will be also sunk (from version 6.d).
626
+
600
627
The C < Empty > constant will act as a no-op for a loop:
601
628
602
629
= for code
You can’t perform that action at this time.
0 commit comments