Difference between Scala method and functio

When I did not learn scala, I mainly used java and python for daily work development. In the impression, there is no special intention to distinguish the difference between method and function. This relationship, just like in our daily life, we do not deliberately distinguish between quality and function. weight. However, there are indeed differences between them. These differences are also based on the connection between them!

   How to define

First of all, let's cite the original English text to see the differences and connections between them:

A Function Type is (roughly) a type of the form (T1, ..., Tn) => U, which is a shorthand for the trait FunctionN in the standard library. Anonymous Functions and Method Values ​​have function types, and function types can be used as part of value, variable and function declarations and definitions. In fact, it can be part of a method type.
A Method Type is a non-value type. That means there is no value-no object, no instance-with a method type. As mentioned above, a Method Value actually has a Function Type. A method type is a def declaration-everything about a def except its body.
scala> def m1 (x: Int) = x + 3
m1: (x: Int) Int

scala> val f1 = (x: Int) => x + 3
f1: Int => Int =
Seeing that, is the method definition and function definition displayed on the scala parser signature? Def m1 (x: Int) = x + 3 is a simple method definition. In the signature, m1: (x: Int) Int means that method m1 has a parameter Int, and the return value is Int.
val f1 = (x: Int) => x + 3 is the definition of function. f1 in the signature of the parser: Int => Int = means that the method body of function f1 accepts an Int type parameter and outputs the result The type is Int.
From the above example, a summary is drawn:
A method is a logical operation block with a parameter list (which can have no parameter list) that starts with def, just like member methods in object or class.
A function is an anonymous method (with or without a parameter list) assigned to a variable (or constant), and it follows an expression in a logical block of code with => conversion symbols. => The writing of the logical code block after the conversion symbol is the same as the body part of the method.

Other differences

method can appear as part of an expression (calling a function and passing parameters), but method (methods with parameters) cannot be used as a final expression (methods without parameters can, but this becomes a method call, because scala allows methods without parameters The () brackets are omitted when called, and function can appear as a final expression.

scala> m1
: 12: error: missing arguments for method m1;
follow this method with `_ 'if you want to treat it as a partially applied function

scala> f1
res1: Int => Int =
method can have no parameter list, and the parameter list can be empty. But function must have a parameter list (it can also be empty). The method name means a method call, and the function name simply represents the function itself:

scala> def m2 = 100;
m2: Int

scala> def m3 () = 1000;
m3: () Int

scala> var f2 = => 100;
: 1: error: illegal start of simple expression
var f2 = => 100;

scala> var f2 = () => 100;
f2: () => Int =

scala> m2
res2: Int = 100

scala> m3
res3: Int = 1000

scala> m3 ()
res4: Int = 1000

scala> f2
res5: () => Int =

scala> f2 ()
res6: Int = 100

We can provide a method where the function appears.

This is because if we provide a method where a function is expected to appear, the method is automatically converted into a function. This behavior is called ETA expansion.


Where functions are expected, we can use methods.

Where functions are not expected, methods are not automatically converted to functions.

Operators are interpreted in scala as methods:

Prefix operator: op obj is interpreted as obj.op
Infix operator: obj1 op obj2 is interpreted as obj1.op (obj2)
Postfix operator: obj op is interpreted as obj.op

scala> val ml = List (1,2,3,4)
ml: List [Int] = List (1, 2, 3, 4)

scala> ml.map ((x) => 2 * x)
res0: List [Int] = List (2, 4, 6, 8)

scala> def m (x: Int) = 2 * x
m: (x: Int) Int

scala> ml.map (m)
res1: List [Int] = List (2, 4, 6, 8)

scala> def m (x: Int) = 3 * x
m: (x: Int) Int

scala> ml.map (m)
res2: List [Int] = List (3, 6, 9, 12)
You can force a function with an underscore after the method name. Note: There must be at least one space between the method name and the underscore!

scala> def m3 (x: Int): Int = x * x * x
m3: (x: Int) Int

scala> val f3 = m3_
: 10: error: not found: value m3_
        val f3 = m3_

scala> val f3 = m3 _
f3: Int => Int =

scala> f3 (3)
res0: Int = 27