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?

OneZero
  • 11,556
  • 15
  • 55
  • 92

4 Answers4

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.

wingedsubmariner
  • 13,350
  • 1
  • 27
  • 52
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
sberry
  • 128,281
  • 18
  • 138
  • 165
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.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
-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.

cchantep
  • 9,118
  • 3
  • 30
  • 41
  • This functional principle works across body of the function, according conditionals/loop; e.g. `if (cond) valReturnedIfCond else valReturnedElse` – cchantep Jul 21 '14 at 00: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 Jul 21 '14 at 00:50
  • @DonRoby you're right, but imperative Scala is of no interest for me – cchantep Jul 21 '14 at 01:01
  • 3
    Regardless of whether you're interested in it, "there is no `return` statement in Scala" simply isn't true. – Chris Martin Jul 21 '14 at 01:29