Practical difference between def f(x: Int) = x+1 and val f = (x: Int) => x+1 in Scala

后端 未结 2 2273
鱼传尺愫
鱼传尺愫 2021-02-20 14:03

I\'m new to Scala and I\'m having a problem understanding this. Why are there two syntaxes for the same concept, and none of them more efficient or shorter at that (merely from

2条回答
  •  广开言路
    2021-02-20 14:46

    There are three main differences (that I know of):

    1. Internal Representation

    Function expressions (aka anonymous functions or lambdas) are represented in the generated bytecode as instances of any of the Function traits. This means that function expressions are also objects. Method definitions, on the other hand, are first class citizens on the JVM and have a special bytecode representation. How this impacts performance is hard to tell without profiling.

    2. Reference Syntax

    References to functions and methods have different syntaxes. You can't just say foo when you want to send the reference of a method as an argument to some other part of your code. You'll have to say foo _. With functions you can just say foo and things will work as intended. The syntax foo _ is effectively wrapping the call to foo inside an anonymous function.

    3. Generics Support

    Methods support type parametrization, functions do not. For example, there's no way to express the following using a function value:

    def identity[A](a: A): A = a
    

    The closest would be this, but it loses the type information:

    val identity = (a: Any) => a
    

提交回复
热议问题