OneZero
OneZero

Reputation: 11904

`return` in a scala function literal

I'm trying to do something like this:

var fun : (Int,Int) => Double = (a,b) =>
{
  // do something
  return 1.0
}

However, my IDE complaints with Return statement outside method definition. So how do I explicitly give a return statement in a function literal in scala?

Upvotes: 2

Views: 3566

Answers (4)

Chris Martin
Chris Martin

Reputation: 30736

how do I explicitly give a return statement in a function literal in scala?

You can't. This is answered by the language specification section 6.20, Return Expressions:

A return expression return e must occur inside the body of some enclosing named method or function.

An apply method which is generated by the compiler as an expansion of an anonymous function does not count as a named function in the source program, and therefore is never the target of a return expression.

Upvotes: 2

wingedsubmariner
wingedsubmariner

Reputation: 13667

In Scala a return statement returns from the enclosing method body. If return appears inside of a function literal, it is implemented with exception throwing. return will throw an exception inside of the function which will then be caught be the enclosing method. Scala works this way in order to make the use of custom control constructs that take functions invisible, for example:

def geometricAverage(l: List[Double]): Double = {
  val product = l.foldLeft(1.0) { (z, x) =>
    if (x == 0) return 0 else z * x
  }
  Math.pow(product, 1.0 / l.length)
}

The return in this example returns from the geometricAverage method, allowing it to complete instantly if a 0 is found. You don't need to know that foldLeft is a method that takes a function rather than a built-in construct to realize this.

The preference in Scala is to write functions in functional style, taking advantage of Scala's expression-oriented nature to make return unnecessary. For example:

val fun: (Int,Int) => Double = (a,b) => {
  if (a == b) {
    // side effect here, if desired
    0.0
  } else {
    // side effect here, if desired
    1.0
  }
}

If you really want to use return in the definition of the function, you can implement the appropriate function interface manually instead of using a function literal:

val fun = new Function2[Int, Int, Double] {
  def apply(x: Int, y: Int): Double = {
    if (x == y)
      return 0.0
    1.0
  }
}

But this is not idiomatic and strongly discouraged.

Upvotes: 10

sberry
sberry

Reputation: 131978

Don't use return, it makes Scala cry.

scala> var fun: (Int, Int) => Double = (a, b) => {
     | // do something
     | 1.0
     | }
fun: (Int, Int) => Double = <function2>

scala> fun(1, 2)
res4: Double = 1.0

Or better yet:

scala> def fun: (Int, Int) => Double = (a, b) => {
     | // do something
     | 1.0
     | }
fun: (Int, Int) => Double

scala> fun(1, 2)
res4: Double = 1.0

Upvotes: 6

cchantep
cchantep

Reputation: 9158

There is no return statement in Scala. In any function the last statement executed is the value returned. If this last statement doesn't comply with expected return type of the function, compiler will raise error.

Upvotes: -9

Related Questions