现在的位置: 首页 > 综合 > 正文

即时函数的return是返回到调用这个函数的块外部,而不是返回到函数返回点。

2013年12月10日 ⁄ 综合 ⁄ 共 289字 ⁄ 字号 评论关闭

如果我们直接在一个继承于App的类中定义:

val f = (x: Int) => { return x; 2 }

会提示我们:return outside method definition,现在我们把它放在一个方法中:

  def outter: Int = {
    val f = (x: Int) => { return x; 2 }
    println("before.")
    f(1)
    println("after.")
    3
  }

  println(outter)

输出:

before.
1
也就是f(1)后面的任何代码都没有执行,到了f(1)中的return  已经返回到它外部的outter而不是返回(x:Int)的返回点。

抱歉!评论已关闭.