2

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?

1

4 Answers 4

10

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.

1
  • return can also be used to return from a named function. Commented Jul 21, 2014 at 1:46
6

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
2

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.

-9

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.

4
  • This functional principle works across body of the function, according conditionals/loop; e.g. if (cond) valReturnedIfCond else valReturnedElse
    – cchantep
    Commented Jul 21, 2014 at 0:12
  • 5
    There is indeed a return statement, and it can be used in imperative style code for early exits and the like. It's frowned upon.
    – Don Roby
    Commented Jul 21, 2014 at 0:50
  • @DonRoby you're right, but imperative Scala is of no interest for me
    – cchantep
    Commented Jul 21, 2014 at 1:01
  • 3
    Regardless of whether you're interested in it, "there is no return statement in Scala" simply isn't true. Commented Jul 21, 2014 at 1:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.