ForExpr

ForExpr is a nonkernel node which executes code for each element in a collection or other object implementing iterate/1.

"for" ( optKeyPattern "=>")? valuePattern "in" collection "{" body "}" ({ optBreakCatch an EMatcher}: "catch" pattern "{" expr "}")?

Fields

 * optKeyPattern :nullOk [ Pattern]
 * valuePattern :Pattern
 * collection :nullOk [ EExpr]
 * body :EExpr
 * optBreakCatch :nullOk [ EMatcher]

Static constraints

 * collection may not use nouns bound by optKeyPattern or valuePattern, or vice versa.

Tests
The function provided to (the value of collection )'s iterate/1 method throws if called after iterate/1 has returned.

? var captured := null ? def capturer { >  to iterate(f) :void { >    captured := f >     f("a", "b") >  } > } ? for x => y in capturer { println(x, " -> ", y) } ? captured("c", "d")
 * 1) value:
 * 1) stdout: a -> b
 * 1) problem: For-loop body isn't valid after for-loop exits.

The return value of iterate/1 is not the return value of the for loop; the loop returns null unless a return value is given by __break.

? def confuser extends [1,2,3] { >  to iterate(f) { >    super.iterate(f) >    return __Portrayal >  } > } ? def optKeyOf(collection, toFind) { >  return for key => val in collection { >    if (val == toFind) { >      break key >    } >   } > } ? optKeyOf(confuser, 1) ? [optKeyOf(confuser, 43)]
 * 1) value:
 * 1) value: 0
 * 1) value: [null]