boundary (original) (raw)
scala.util.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:
boundary
to establish a scope,break
to leave it.break
can optionally return a value. - Integration with exceptions.
break
s are logically non-fatal exceptions. TheBreak
exception class extendsRuntimeException
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
Attributes
Source
Graph
Supertypes
Self type
Members list
User code should call break.apply
instead of throwing this exception directly.
User code should call break.apply
instead of throwing this exception directly.
Attributes
Source
Supertypes
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
Supertypes
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.
Attributes
Source
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
.
Attributes
Source
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
.
Attributes
Source
In this article