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)
}
- Companion:
- object
- Source:
- Breaks.scala
Type members
Classlikes
Value members
Concrete methods
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".
- Source:
- Breaks.scala
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
.
- Source:
- Breaks.scala
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
}
- Source:
- Breaks.scala