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)
}
Attributes
- Companion
- object
- Source
- Breaks.scala
- Graph
-
- Supertypes
- Known subtypes
-
object Breaks.type