Packages

class Breaks extends AnyRef

Provides the break control abstraction.

The break method uses a ControlThrowable to transfer control up the stack to an enclosing breakable.

It is typically used to abruptly terminate a for loop, but can be used to return from an arbitrary computation.

Control resumes after the breakable.

If there is no matching breakable, the BreakControl thrown by break is handled in the usual way: if not caught, it may terminate the current Thread.

BreakControl carries no stack trace, so the default exception handler does not print useful diagnostic information; there is no compile-time warning if there is no matching breakable.

A catch clause using NonFatal is safe to use with break; it will not short-circuit the transfer of control to the enclosing breakable.

A breakable matches a call to break if the methods were invoked on the same receiver object, which may be the convenience value Breaks.

Example usage:

val mybreaks = new Breaks
import mybreaks.{break, breakable}

breakable {
  for (x <- xs) {
    if (done) break()
    f(x)
  }
}

Calls to break from one instance of Breaks will never resume at the breakable of some other instance.

Any intervening exception handlers should use NonFatal, or use Try for evaluation:

val mybreaks = new Breaks
import mybreaks.{break, breakable}

breakable {
  for (x <- xs) Try { if (quit) break else f(x) }.foreach(println)
}
Source
Breaks.scala
Linear Supertypes
Known Subclasses
Type Hierarchy
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Breaks
  2. AnyRef
  3. Any
Implicitly
  1. by any2stringadd
  2. by StringFormat
  3. by Ensuring
  4. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Instance Constructors

  1. new Breaks()

Type Members

  1. sealed trait TryBlock[T] extends AnyRef

Value Members

  1. def break(): Nothing

    Break from the dynamically closest enclosing breakable block that also uses this Breaks instance.

    Break from the dynamically closest enclosing breakable block that also uses this Breaks instance.

    Note

    This might be different from the statically closest enclosing block!

    ,

    Invocation without parentheses relies on the conversion to "empty application".

  2. def breakable(op: => Unit): Unit

    A block from which one can exit with a break.

    A block from which one can exit with a break. The break may be executed further down in the call stack provided that it is called on the exact same instance of Breaks.

  3. def tryBreakable[T](op: => T): TryBlock[T]

    Try a computation that produces a value, supplying a default to be used if the computation terminates with a break.

    Try a computation that produces a value, supplying a default to be used if the computation terminates with a break.

    tryBreakable {
      (1 to 3).map(i => if (math.random < .5) break else i * 2)
    } catchBreak {
      Vector.empty
    }