Scala method types and methods as parameters

前端 未结 3 1745
我在风中等你
我在风中等你 2021-01-18 10:26

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

相关标签:
3条回答
  • 2021-01-18 10:50

    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?

    0 讨论(0)
  • 2021-01-18 10:55

    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
    
    0 讨论(0)
  • 2021-01-18 11:03

    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.

    0 讨论(0)
提交回复
热议问题