A boundary that can be exited by break calls. boundary and break represent a unified and superior alternative for the scala.util.control.NonLocalReturns and scala.util.control.Breaks APIs. The main differences are:
Unified names: boundary to establish a scope, break to leave it. break can optionally return a value.
Integration with exceptions. breaks are logically non-fatal exceptions. The Break exception class extends RuntimeException and is optimized so that stack trace generation is suppressed.
Better performance: breaks to enclosing scopes in the same method can be rewritten to jumps.
Example usage:
import scala.util.boundary, boundary.break
def firstIndex[T](xs: List[T], elem: T): Int =
boundary:
for (x, i) <- xs.zipWithIndex do
if x == elem then break(i)
-1
inline def apply[T](inline body: (Label[T]) ?=> T): T
Run body with freshly generated label as implicit argument. Catch any breaks associated with that label and return their results instead of body's result.
Run body with freshly generated label as implicit argument. Catch any breaks associated with that label and return their results instead of body's result.