Design decisions

126. Library loops are generators; early exit stays in for (2026-11-21)

  • Decision. Loop-shaped library facilities (traversals, enumerations, divisors, submasks, enter/exit walks) are generators consumed with a plain for. break, continue, return, labelled break 'outer and loop else (§125) work in that body even when the generator is recursive: the compiler inlines a non-recursive push generator to a jump, and threads a hidden stop path through the frames of a recursive one, only in specializations whose body can exit (cpl explain shows it). Enter/exit walks yield events (Enter(v) / Exit(v)). A body that steers the generator uses a method on the yielded item, read by the generator after the yield returns (step.prune() skips a subtree); this is ordinary library code. Lockstep over recursive generators is List(g) (4.2's existing error). Folds and predicates stay closures (t.up(+, finish:), bsearch_first).
  • Deferred. return / break inside a closure literal acting on the enclosing function (Kotlin's inline lambdas, Ruby blocks). Revisit only if ports keep needing early exit from closure-shaped library calls.
  • Why. Library loops get exactly the control flow of built-in loops with no per-library protocol (no ControlFlow values, no found flags), which is the no-privileged-stdlib test; and no closure ever has two meanings of return.
  • Reverse. Allow closure-level non-local exit for call-only closure parameters, or require library loops to accept a stop signal.
  • Status. Accepted. break and return out of a for over a recursive yields fn already run identically in cpl run and the C++. Not yet done: item-steered prune, Enter/Exit events, labelled break and loop else coverage over generators, and converting the remaining callback-shaped stdlib traversals.