Permalink
Browse files

Okay, statement 'for' is now also sunk.

Now Perl 6 is perfect.
  • Loading branch information...
1 parent 96e17b9 commit beef8e630be2440050c979252db56af227c910f2 @TimToady TimToady committed Nov 26, 2013
Showing with 19 additions and 20 deletions.
  1. +19 −20 S04-control.pod
View
@@ -13,7 +13,7 @@ Synopsis 4: Blocks and Statements
Created: 19 Aug 2004
Last Modified: 26 Nov 2013
- Version: 131
+ Version: 132
This document summarizes Apocalypse 4, which covers the block and
statement syntax of Perl.
@@ -667,33 +667,20 @@ The final statement of a statement list is not a sink context, and
can return any value including a lazy list. However, to support the
expectations of imperative programmers (the vast majority of us,
it turns out), any explicit loop found as the final statement of
-a statement list is automatically forced to use eager semantics so
+a statement list is automatically forced to use sink semantics so
that the loop executes to completion before returning from the block.
-In particular, a C<for> loop still returns the loop results as a list,
-but only after the loop completes. (To suppress even this list return,
-put a return statement after the C<for> loop, or declare the subroutine
-with a return type that indicates no return value is expected.)
-
-For other loops such as C<while>, C<repeat>, and C<loop>, not only
-are they assumed eager, the loops will also be assumed to be in sink
-context at statement level.
-
-This forced eager context (and sink context, if applicable)
-is applied to loops I<only> at the statement list
+This forced sink context is applied to loops I<only> at the statement list
level, that is, at the top level of a compilation unit, or directly
inside a block. Constructs that parse a single statement or semilist
as an argument are presumed to want the results of that statement,
so such constructs remain lazy even when that statement is a loop.
Assuming each of the following statements is the final statement in a block,
-eager loops such as these may be indicated:
+"sunk" loops such as these may be indicated:
- # assumed eager context
for LIST { ... }
- ... if COND for LIST # eager list comprehension
-
- # assumed sink context
- loop { ... } # (assumes sink in addition to eager)
+ ... if COND for LIST
+ loop { ... }
... while COND
while COND { ... }
repeat until COND { ... }
@@ -710,14 +697,26 @@ or by use of either a statement prefix or a phaser in statement form:
ENTER for LIST { ... }
Note that the corresponding block forms put the loop into a statement list, so
-these loops are eagerly evaluated:
+these loops are evaluated in sink context:
lazy { for LIST { ... } } # useless use of 'lazy' here
ENTER { for LIST { ... } }
It doesn't matter that there is only one statement there; what matters
is that a sequence of statements is expected there by the grammar.
+An eager loop may likewise be indicated by using the C<eager> statement prefix:
+
+ eager for LIST { ... }
+ eager ... if COND for LIST
+ eager loop { ... }
+ eager ... while COND
+ eager while COND { ... }
+ eager repeat until COND { ... }
+
+It is erroneous to write an eager loop without a loop exit, since that will chew up
+all your memory.
+
Note that since C<do> is considered a one-time loop, it is always
evaluated eagerly, despite being a statement prefix. This is no great
hardship; the C<lazy> prefix is better documentation in any case.

0 comments on commit beef8e6

Please sign in to comment.