【发布时间】:2013-07-17 15:31:32
【问题描述】:
例如,如果我们有一个类似的方法
def find[A](xs: Seq[A], p: A => Boolean): Option[A] = {
xs.foreach(x => if (p(x)) return Some(x));
None;
}
(当然有一个库函数,这只是一个例子)。内部函数returns时,执行如何逃逸foreach?
或者在
def foo(x: AnyRef): String =
process(x match {
case (s: String) => s;
case _ => return "";
})
在发出return "" 时,执行如何避免运行process?
【问题讨论】:
-
Scala uses exceptions for flow control like this(要搜索的关键字是
NonLocalReturnControl)。另见 Scala 语言规范,第 6.20 节
标签: scala return implementation anonymous-function