Search code examples
scalaeta-expansion

What is the eta expansion in Scala?


I am new to Scala. I just heard the term "eta expansion" and roughly know that it means to expand a method to a function object. But I find few resources in SO that systematically introduce it.

I am curious about how eta expansion works in Scala. What are the scenarios that eta expansion are needed? And how eta expansion is implemented in Scala?

I roughly know that in cases like this:

def someMethod(x: Int): Int = x * x

someMethod _ will roughly be translated to a new function object like this:

new Function1[Int, Int] {
  def apply(x: Int): Int = x * x
}

Is it all that Scala does?


Solution

  • The definition, and some examples, are given in http://scala-lang.org/files/archive/spec/2.11/06-expressions.html#method-values.

    someMethod _ will roughly be translated to a new function object like this:

    Not quite: it's actually

    new Function1[Int, Int] {
      def apply(x: Int): Int = someMethod(x)
    }
    

    The difference matters e.g. if someMethod is overridden somewhere.

    Is it all that Scala does?

    You also need to take into account what happens if the method takes multiple parameter lists (you get a function which returns a function) or by-name parameters.

    What are the scenarios that eta expansion are needed?

    1. When you specifically ask for it (e.g. someMethod _).

    2. When you use a method (with parameters) where a value of a function type (or a SAM type in Scala 2.12) is expected. E.g.

      def foo(f: Int => Int) = ???
      
      foo(someMethod)
      
    3. That's it.

    Note that using eta-expansion and an anonymous function with placeholders (someMethod(_)) can behave differently due to type inference, implicits, etc.