boundary
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:
boundaryto establish a scope,breakto leave it.breakcan optionally return a value. - Integration with exceptions.
breaks are logically non-fatal exceptions. TheBreakexception class extendsRuntimeExceptionand 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
Attributes
- Source
- boundary.scala
- Graph
-
- Supertypes
- Self type
-
boundary.type
Members list
Type members
Classlikes
User code should call break.apply instead of throwing this exception directly.
User code should call break.apply instead of throwing this exception directly.
Note that it is capability unsafe to access label from a Break. This field will be marked private in a future release.
Type parameters
- T
-
the type of the value carried by this
Breakexception
Attributes
- Companion
- object
- Source
- boundary.scala
- Supertypes
-
class RuntimeExceptionclass Exceptionclass Throwabletrait Serializableclass Objecttrait Matchableclass AnyShow all
Labels are targets indicating which boundary will be exited by a break.
Labels are targets indicating which boundary will be exited by a break.
Attributes
- Source
- boundary.scala
- Supertypes
-
trait Controltrait SharedCapabilitytrait Classifiertrait Capabilityclass Objecttrait Matchableclass AnyShow all
Value members
Concrete methods
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.
Type parameters
- T
-
the result type of the boundary block
Value parameters
- body
-
the computation to execute, which receives a fresh
Label[T]as a context parameter
Attributes
- Source
- boundary.scala
Abort current computation and instead return value as the value of the enclosing boundary call that created label.
Abort current computation and instead return value as the value of the enclosing boundary call that created label.
Type parameters
- T
-
the type of the value to return from the enclosing
boundary
Value parameters
- label
-
the label identifying the target
boundaryto exit - value
-
the value to return from the enclosing
boundarycall
Attributes
- Source
- boundary.scala
Abort current computation and instead continue after the boundary call that created label.
Abort current computation and instead continue after the boundary call that created label.
Value parameters
- label
-
the label identifying the target
boundaryto exit
Attributes
- Source
- boundary.scala