Skip to content

Commit 7cb3236

Browse files
committed
Illustrating last for statement in sink context #2632
1 parent ab254c9 commit 7cb3236

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

doc/Language/control.pod6

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,33 @@ assign them to an array:
597597
my @a = do for 1, 2, 3 { $_ * 2 }; @a.say; # OUTPUT: «[2 4 6]␤»
598598
my @b = (for 1, 2, 3 { $_ * 2 }); @b.say; # OUTPUT: «[2 4 6]␤»
599599
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+
600627
The C<Empty> constant will act as a no-op for a loop:
601628
602629
=for code

0 commit comments

Comments
 (0)