Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala method types and methods as parameters

In the following code example, I do not understand why the function fun can be passed as an argument to the method addAction. The method fun is of type Unit, while the method addAction expects a function of type () => Unit.

If fun is of type () => Unit, then why does the compiler complain that fun is of type Unit, when I try to add fun to the actions list: actions = fun :: actions?

package myscala

object MyScala {

  def fun() { println("fun1 executed.") }

  def addAction(a: () => Unit) {
    actions = a :: actions
  }

  var actions: List[() => Unit] = List()

  def main(args: Array[String]) {
    // the following line would produce a compiler error (found: Unit, required: () => Unit), it's OK
    // actions = fun :: actions
    actions = (() => fun) :: actions // OK
    // I would expect the same compiler error here (found: Unit, required: () => Unit), but it's OK why?
    addAction(fun)
    actions.foreach(_()) // prints twice "fun1 executed"
  }
}
like image 990
Manu Avatar asked Oct 07 '11 17:10

Manu


3 Answers

Take this as an introductory example:

def fun() { println("fun1 executed.") }

val a1 = fun
val a2: () => Unit = fun

Both lines compile and (thanks to type inference) they look equivalent. However a1 is of type Unit while a2 is of type () => Unit... How is this possible?

Since you are not explicitly providing type of a1, compilers interprets fun as a method fun call of type Unit, hence the type of a1 is the same as type of fun. It also means that this line will print fun1 executed.

However, a2 has explicitly declared type of () => Unit. The compiler helps you here and it understands that since the context requires a function of type () => Unit and you provided a method matching this type, it shouldn't call that method, but treat it as first class function!

You are not doomed to specify type of a1 explicitly. Saying:

val a1 = fun _

Do you now understand where your problem is?

like image 130
Tomasz Nurkiewicz Avatar answered Oct 17 '22 07:10

Tomasz Nurkiewicz


You need to write fun _ in the first case to avoid calling the method and performing eta-expansion instead.

This will work:

actions = (fun _) :: actions

If you don't do this, then fun is evaluated.

For more details, see Section 6.7 (Method Values) of the Scala Language Reference.

As to why fun is not evaluated in the second case, it is because type inference can clearly conclude that addAction expects a function. By the way, the type of fun is technically ()Unit, not Unit, that is, a method type, and not a value type. See Section 3.3.1 in the reference for more.

like image 43
Philippe Avatar answered Oct 17 '22 07:10

Philippe


There is a difference between methods and functions. In your case actions is a list of functions. When the compiler knows that a function is required (like in the case of addAction) it can automatically convert a method fun into a function. Now :: is also a method, therefore the compiler also knows that it takes functions as parameters. But the problem is the syntactic sugar of the right-associative operator ::. If you were to call it like a method: actions.::(fun) it will compile (although I can't test it at the moment). When writing fun :: actions the compiler thinks that fun is an expression and therefore evaluates it and since it "returns" a Unit you get your compiler error.

EDIT

Since I now have the possibility to test my hypothesis (which was wrong) here are your options:

// Usual syntax
actions.::[() => Unit](fun)
actions.::(fun: () => Unit)
actions.::(fun _)
// Operator syntax
(fun: () => Unit) :: actions
(fun _) :: actions
like image 3
agilesteel Avatar answered Oct 17 '22 07:10

agilesteel