Scala return statements in anonymous functions

橙三吉。 提交于 2019-11-28 08:05:01
James Iry

Formally speaking return is defined as always returning from the nearest enclosing named method

A return expression return e must occur inside the body of some enclosing named method or function. The innermost enclosing named method or function in a source program, f , must have an explicitly declared result type, and the type of e must conform to it. The return expression evaluates the expression e and returns its value as the result of f . The evaluation of any statements or expressions following the return expression is omitted.

So it doesn't have different semantics in a lambda. The wrinkle is that, unlike a normal method, a closure created from a lambda can escape a call to the enclosing method and you can get an exception if there is a return in such a closure.

If the return expression is itself part of an anonymous function, it is possible that the enclosing instance of f has already returned before the return expression is executed. In that case, the thrown scala.runtime.NonLocalReturnException will not be caught, and will propagate up the call stack.

Now, as for "why". One lesser reason is aesthetic: lambdas are expressions and it's nice when an expression and all its subexpression have the same meaning no matter what the nesting structure. Neal Gafter talks about that at http://gafter.blogspot.com/2006/08/tennents-correspondence-principle-and.html

The main reason it exists, though, is it allows you to easily simulate forms of control flow commonly used in imperative programming but still allows you to abstract things into higher order functions. As a toy example, Java's foreach construct (for (x : xs) { yada; }) allows a return inside the loop. Scala doesn't have a language level foreach. Instead, it puts foreach in the library (not counting "for expression" without yield since they just desugar to foreach). Having a non-local return means you can take a Java foreach and translate directly to a Scala foreach.

BTW, Ruby, Smalltalk, and Common Lisp (off the top of my head) also have similar "non-local" returns.

0__

The return keyword is reserved for (class) methods, it cannot be used in functions. You can easily test that:

object Foo {
  val bar = (i: Int) => return i + i
}

This gives

<console>:42: error: return outside method definition
       object Foo { val bar = (i: Int) => return i + i }
                                          ^

Mostly you can treat methods and functions as the same, because of the function's apply method behaving syntactically like calling a method, and so-called eta-expansion allowing a method to be passed as a function argument.

In this case, it makes a difference. When defining as method, it is legal:

object Foo { 
  def bar(i: Int): Int = return i + i
}

In summary, you should only use return in methods that allow conditional (early) returns. See this post for a discussion on methods versus functions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!