Skip to content

Commit beef8e6

Browse files
committed
Okay, statement 'for' is now also sunk.
Now Perl 6 is perfect.
1 parent 96e17b9 commit beef8e6

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

S04-control.pod

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Synopsis 4: Blocks and Statements
1313
Created: 19 Aug 2004
1414

1515
Last Modified: 26 Nov 2013
16-
Version: 131
16+
Version: 132
1717

1818
This document summarizes Apocalypse 4, which covers the block and
1919
statement syntax of Perl.
@@ -667,33 +667,20 @@ The final statement of a statement list is not a sink context, and
667667
can return any value including a lazy list. However, to support the
668668
expectations of imperative programmers (the vast majority of us,
669669
it turns out), any explicit loop found as the final statement of
670-
a statement list is automatically forced to use eager semantics so
670+
a statement list is automatically forced to use sink semantics so
671671
that the loop executes to completion before returning from the block.
672672

673-
In particular, a C<for> loop still returns the loop results as a list,
674-
but only after the loop completes. (To suppress even this list return,
675-
put a return statement after the C<for> loop, or declare the subroutine
676-
with a return type that indicates no return value is expected.)
677-
678-
For other loops such as C<while>, C<repeat>, and C<loop>, not only
679-
are they assumed eager, the loops will also be assumed to be in sink
680-
context at statement level.
681-
682-
This forced eager context (and sink context, if applicable)
683-
is applied to loops I<only> at the statement list
673+
This forced sink context is applied to loops I<only> at the statement list
684674
level, that is, at the top level of a compilation unit, or directly
685675
inside a block. Constructs that parse a single statement or semilist
686676
as an argument are presumed to want the results of that statement,
687677
so such constructs remain lazy even when that statement is a loop.
688678
Assuming each of the following statements is the final statement in a block,
689-
eager loops such as these may be indicated:
679+
"sunk" loops such as these may be indicated:
690680

691-
# assumed eager context
692681
for LIST { ... }
693-
... if COND for LIST # eager list comprehension
694-
695-
# assumed sink context
696-
loop { ... } # (assumes sink in addition to eager)
682+
... if COND for LIST
683+
loop { ... }
697684
... while COND
698685
while COND { ... }
699686
repeat until COND { ... }
@@ -710,14 +697,26 @@ or by use of either a statement prefix or a phaser in statement form:
710697
ENTER for LIST { ... }
711698

712699
Note that the corresponding block forms put the loop into a statement list, so
713-
these loops are eagerly evaluated:
700+
these loops are evaluated in sink context:
714701

715702
lazy { for LIST { ... } } # useless use of 'lazy' here
716703
ENTER { for LIST { ... } }
717704

718705
It doesn't matter that there is only one statement there; what matters
719706
is that a sequence of statements is expected there by the grammar.
720707

708+
An eager loop may likewise be indicated by using the C<eager> statement prefix:
709+
710+
eager for LIST { ... }
711+
eager ... if COND for LIST
712+
eager loop { ... }
713+
eager ... while COND
714+
eager while COND { ... }
715+
eager repeat until COND { ... }
716+
717+
It is erroneous to write an eager loop without a loop exit, since that will chew up
718+
all your memory.
719+
721720
Note that since C<do> is considered a one-time loop, it is always
722721
evaluated eagerly, despite being a statement prefix. This is no great
723722
hardship; the C<lazy> prefix is better documentation in any case.

0 commit comments

Comments
 (0)