A boundary that can be exited bybreak calls.boundary andbreak represent a unified and superior alternative for thescala.util.control.NonLocalReturns andscala.util.control.Breaks APIs. The main differences are:
boundary to establish a scope,break to leave it.break can optionally return a value.breaks are logically non-fatal exceptions. TheBreak exception class extendsRuntimeException and is optimized so that stack trace generation is suppressed.Example usage:
import scala.util.boundary, boundary.breakdef firstIndex[T](xs: List[T], elem: T): Int = boundary: for (x, i) <- xs.zipWithIndex do if x == elem then break(i) -1User code should callbreak.apply instead of throwing this exception directly.
User code should callbreak.apply instead of throwing this exception directly.
Labels are targets indicating which boundary will be exited by abreak.
Labels are targets indicating which boundary will be exited by abreak.
Runbody with freshly generated label as implicit argument. Catch any breaks associated with that label and return their results instead ofbody's result.
Runbody with freshly generated label as implicit argument. Catch any breaks associated with that label and return their results instead ofbody's result.
Abort current computation and instead returnvalue as the value of the enclosingboundary call that createdlabel.
Abort current computation and instead returnvalue as the value of the enclosingboundary call that createdlabel.
Abort current computation and instead continue after theboundary call that createdlabel.
Abort current computation and instead continue after theboundary call that createdlabel.