The difference between functions and methods in Scala

1. Different definitions

1.1 method with def

def methodName1 (p1:Int, p2:Int) : Int = {

val c = a+b

return c

}

1.2 Function use var

var funcName1 = (p1:Int, p2:Int) => p1 +p2

 

2. The method name is the `` call '' method itself,

The function name `` represents '' the function itself

 

3. Parameter list () can not be omitted

3.1 The parameter list of the method can be omitted

def methodName2 :Int={
  var c = 3
  print("this is 3")
return c
}

 

3.2 Function parameter list cannot be omitted

var funcName2 = ()  => print("test")

Put parentheses even if there are no parameters

 

4. Can you specify the return value type

Method can be specified

Function cannot be specified

 

detailed:

The method in Scala is the same as the method in Java, and the method is part of the composing class. Methods have names, type signatures, and sometimes annotations on the methods, as well as the function implementation code (bytecode) of the method.

A function in Scala is a complete object. Scala uses 22 traits to abstract the concept of functions. These 22 traits are 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 a function literal

In fact, the definition of the above function is equivalent to the following definition:

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

apply is syntactic sugar in scala: if you call obj() on an object obj, the scala compiler will convert it to obj.apply(); if you call clazz() on a class clazz, the scala compiler will convert it to clazz_company_obj.apply( ), where clazz_company_obj is the companion object of clazz.

 

The specific differences can be summarized as follows:

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

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, because f itself is

An object (an object that implements FunctionN traits), so this way of using it is completely correct. But if we use the method name as the final expression, an error will occur.

2. The function must have a parameter list, and the method can have no parameter list

In the above example, the m1 method accepts zero parameters, so the parameter list can be omitted. And the function cannot omit the parameter list

3. The method name is a method call, and 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) is itself an object of the class that implements the FunctionN trait, to call the apply method of the object, you need to use the obj() syntax. So add parentheses after the function name to call the function. as follows:

4. Where a function is needed, if a method is passed, ETA expansion will be performed automatically (convert the method to a function)

If we directly assign a method to a variable, an error will be reported. If we specify that the variable type 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. By name parameter is essentially a method

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

The above code actually defines a method m1, and the parameter of m1 is a parameter by name (method). For methods with empty parameters, the method name is the method call, so List(x,x) actually makes two method calls.

Since List(x,x) has made two method calls, two different values ​​are obtained.

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

Guess you like

Origin blog.csdn.net/hzp666/article/details/114658510