Current location - Quotes Website - Signature design - The difference between methods and functions in scala
The difference between methods and functions in scala

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 their differences.

Methods in Scala are the same as methods in Java. Methods are part of the class. Methods have names, type signatures, and sometimes annotations on methods, as well as method functions

implementation code (bytecode).

A function in Scala is a complete object. Scala uses 22 traits to abstract the concept of functions. These 22 traits range from Function1 to Function22

As shown in the figure above, Function10 represents a function with 10 formal parameters and a return value of R (covariant).

Functions in Scala are actually objects of classes that inherit these Traits. For example: we define a function through function literals

In fact, the definition of the above function is the same as the following definition. :

Because Function2 is a trait, it cannot be new directly. The above new Function2[Int,Int,Int](){} actually defines and instantiates an object of a class that implements the Function2 trait.

Apply is syntactic sugar in scala: calling obj() on an object obj, the scala compiler will convert it to obj.apply(); calling clazz() on a class clazz, the scala compiler It will be converted

to clazz_company_obj.apply(), where clazz_company_obj is the companion object of clazz.

The specific differences are summarized as follows:

1. Methods cannot exist as separate expressions (except for methods with empty parameters), but functions can. For example:

In the above example, we first define a method m, and then define a function f. Then we use the function name (function value) as the final expression. Since f itself is an object (an object that implements the FunctionN trait), this usage is completely correct. But if we use the method name as the final expression, an error will occur.

2. Functions must have a parameter list, but methods can have no parameter list

In the above example, the m1 method accepts zero parameters, so the parameter list can be omitted. The parameter list cannot be omitted for a function

3. The method name is used for the method, while the function name only represents the function object itself

This is easier to understand. Because the variable that holds the function literal (also known as the function name or function value) itself is an object of the class that implements the FunctionN trait, to call the apply method of the object, you need to use the syntax of obj() . So adding parentheses after the function name is the calling function. As follows:

4. Where a function is required, if a method is passed, ETA expansion (converting the method into a function) will be automatically performed.

If we directly assign a method to a variable An error will be reported. 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 application functions in scala:

5. The parameter passed by name is essentially a method

The parameter passed by name is essentially a method with an empty parameter list, as follows:

The above code actually defines a Method m1, the parameter of m1 is a named parameter (method). Since for a method with empty parameters, the method name is the method call

, so List(x,x) actually makes two method calls.

Since List(x,x) makes two method calls, it gets two different values.

If we slightly modify the definition of m1 of the function and cache x first, the result will be very different from before.