boundary

scala.util.boundary
object 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. 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

Attributes

Source
boundary.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
boundary.type

Members list

Type members

Classlikes

final class Break[T] extends RuntimeException

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
boundary.scala
Supertypes
class Exception
class Throwable
trait Serializable
class Object
trait Matchable
class Any
Show all
final class Label[-T]

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
class Object
trait Matchable
class Any

Value members

Concrete methods

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.

Attributes

Source
boundary.scala
def break[T](value: T)(using label: Label[T]): Nothing

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
boundary.scala
def break()(using label: Label[Unit]): Nothing

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
boundary.scala