@@ -2250,6 +2250,14 @@ do_expr : "do" expr [ '|' ident_list '|' ] ? '{' block '}' ;
2250
2250
A _ do expression_ provides a more-familiar block-syntax for a [ lambda expression] ( #lambda-expressions ) ,
2251
2251
including a special translation of [ return expressions] ( #return-expressions ) inside the supplied block.
2252
2252
2253
+ Any occurrence of a [ return expression] ( #return-expressions )
2254
+ inside this ` block ` expression is rewritten
2255
+ as a reference to an (anonymous) flag set in the caller's environment,
2256
+ which is checked on return from the ` expr ` and, if set,
2257
+ causes a corresponding return from the caller.
2258
+ In this way, the meaning of ` return ` statements in language built-in control blocks is preserved,
2259
+ if they are rewritten using lambda functions and ` do ` expressions as abstractions.
2260
+
2253
2261
The optional ` ident_list ` and ` block ` provided in a ` do ` expression are parsed as though they constitute a lambda expression;
2254
2262
if the ` ident_list ` is missing, an empty ` ident_list ` is implied.
2255
2263
@@ -2296,19 +2304,15 @@ A _for expression_ is similar to a [`do` expression](#do-expressions),
2296
2304
in that it provides a special block-form of lambda expression,
2297
2305
suited to passing the ` block ` function to a higher-order function implementing a loop.
2298
2306
2299
- Like a ` do ` expression, a ` return ` expression inside a ` for ` expresison is rewritten,
2300
- to access a local flag that causes an early return in the caller.
2301
-
2302
- Additionally, any occurrence of a [ return expression] ( #return-expressions )
2303
- inside the ` block ` of a ` for ` expression is rewritten
2304
- as a reference to an (anonymous) flag set in the caller's environment,
2305
- which is checked on return from the ` expr ` and, if set,
2306
- causes a corresponding return from the caller.
2307
- In this way, the meaning of ` return ` statements in language built-in control blocks is preserved,
2308
- if they are rewritten using lambda functions and ` do ` expressions as abstractions.
2307
+ In contrast to a ` do ` expression, a ` for ` expression is designed to work
2308
+ with methods such as ` each ` and ` times ` , that require the body block to
2309
+ return a boolean. The ` for ` expression accommodates this by implicitly
2310
+ returning ` true ` at the end of each block, unless a ` break ` expression
2311
+ is evaluated.
2309
2312
2310
- Like ` return ` expressions, any [ ` break ` ] ( #break-expressions ) and [ ` loop ` ] ( #loop-expressions ) expressions
2311
- are rewritten inside ` for ` expressions, with a combination of local flag variables,
2313
+ In addition, [ ` break ` ] ( #break-expressions ) and [ ` loop ` ] ( #loop-expressions ) expressions
2314
+ are rewritten inside ` for ` expressions in the same way that ` return ` expressions are,
2315
+ with a combination of local flag variables,
2312
2316
and early boolean-valued returns from the ` block ` function,
2313
2317
such that the meaning of ` break ` and ` loop ` is preserved in a primitive loop
2314
2318
when rewritten as a ` for ` loop controlled by a higher order function.
0 commit comments