#scala
When a function value containing a return
statement is evaluated nonlocally, the computation is abandoned and the result is returned by throwing a NonLocalReturnControl[A]
.
If you want to change selection, open document below and click on "Move attachment"
tpolecat it returns from the method it appears in. Another example:
def foo: Int = {
val sumR: List[Int] => Int = _.foldLeft(0)((n, m) => return n + m)
sumR(List(1,2,3)) + sumR(List(4,5,6))
}
scala> foo
res4: Int = 1
Non-Local Return
<span>When a function value containing a return statement is evaluated nonlocally, the computation is abandoned and the result is returned by throwing a NonLocalReturnControl[A]. This implementation detail escapes into the wild without much ceremony:
def lazily(s: => String): String =
try s catch { case t: Throwable => t.toString }
def foo: String = laz Summary
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
Details