There are both functions and methods in Scala, and in most cases we can ignore the difference between them. But sometimes we have to understand the difference between them.
The methods in Scala are the same as in Java, and methods are part of the composition class. Methods have names, type signatures, and sometimes annotations on methods, as well as the function of methods
Implementation code (byte code).
The function in Scala is a complete object. Scala uses 22 traits (trait) to abstract the concept of a function. These 22 traits from Function1 to Function22
As in the FUNCTION10, there are 10 parameters, and the return value is r (covariant) function.
The functions in Scala are actually objects that inherit these trait classes, such as: we define a function by the function literal.
In fact, the above function is defined in the same way as the following definition:
Since Function2 is a trait, it cannot be directly new. The above new Function2[int,int,int] {} is actually an object that defines and instantiates a class that implements the Function2 trait.
Apply is the syntactic sugar in Scala: On an object obj is raised with obj (), the Scala compiler is converted to obj.apply (), and the Scala compiler goes to the Clazz () on a class Clazz
Change to Clazz_company_obj.apply (), where Clazz_company_obj is the associated object of Clazz.
The specific differences, summed up as the following points:
1. A method cannot exist as a separate expression (except for a method with null parameters), whereas a function can. Such as:
In the example above, we first define a method m, then a function f is defined. Then we use the function name as the final expression, because F itself is
An object (an object that implements the Functionn trait), so this is exactly the right way to use it. But if we use the method name as the final expression, we will make an error.
2. The function must have a parameter list, and the method can have no argument list
In the example above, the M1 method accepts 0 parameters, so you can omit the argument list. And the function cannot omit the argument list
3. The method name is the method bar, and the function name simply represents the function object itself
This is easier to understand. Because the variable that holds the function literal (also called the function name or function value) itself is the object of the class that implements the Functionn trait, the object to invoke the Apply
method, you need to use the syntax of obj (). So parentheses after the function name are the calling function. As follows:
4. Where a function is required, if a method is passed, an ETA expansion is performed automatically (converting the method to a function)
If we assign a method directly to the variable it will give an error. If we specify that the type of the variable is a function, then it can be compiled, as follows:
Of course we can also force a method to be converted to a function, which uses some of the application functions in Scala:
5. The Bogwang parameter is essentially a method
The Bogwang parameter is essentially a method that has an empty argument list, as follows:
As the above code actually defines a method, the M1,m1 parameter is a Bogwang parameter (method). Because the method name is the method call for a method with null parameters
, so list (x,x) is actually making two method calls.
Because list (x,x) is a two-time method call, it gets two different values.
If we modify the M1 definition of the function a little bit, we will cache the x first, and the result would be very different from the previous one.
Translation The difference between Scala methods and functions