File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed
library/src/scala/util/control Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ package scala .util .control
2+
3+ /** Library implementation of nonlocal return.
4+ *
5+ * Usage:
6+ *
7+ * import scala.util.control.NonLocalReturns._
8+ *
9+ * returning { ... throwReturn(x) ... }
10+ */
11+ object NonLocalReturns {
12+ class ReturnThrowable [T ] extends ControlThrowable {
13+ private var myResult : T = _
14+ def throwReturn (result : T ): Nothing = {
15+ myResult = result
16+ throw this
17+ }
18+ def result : T = myResult
19+ }
20+
21+ /** Performs a nonlocal return by throwing an exception. */
22+ def throwReturn [T ](result : T ) given (returner : ReturnThrowable [T ]): Nothing =
23+ returner.throwReturn(result)
24+
25+ /** Enable nonlocal returns in `op`. */
26+ def returning [T ](op : given ReturnThrowable [T ] => T ): T = {
27+ val returner = new ReturnThrowable [T ]
28+ try op given returner
29+ catch {
30+ case ex : ReturnThrowable [T ] =>
31+ if (ex.eq(returner)) ex.result else throw ex
32+ }
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ import scala .util .control .NonLocalReturns ._
2+
3+ object Test {
4+ def has (xs : List [Int ], elem : Int ) =
5+ returning {
6+ for (x <- xs)
7+ if (x == elem) throwReturn(true )
8+ false
9+ }
10+
11+ def main (arg : Array [String ]): Unit = {
12+ assert(has(1 :: 2 :: Nil , 1 ))
13+ assert(has(1 :: 2 :: Nil , 2 ))
14+ assert(! has(1 :: 2 :: Nil , 3 ))
15+ }
16+ }
You can’t perform that action at this time.
0 commit comments