Before I learned Scala, I mainly used Java and Python for daily development work. In my impression, I didn't deliberately distinguish between methods and functions. This is like how we don't deliberately distinguish between mass and weight in our daily lives. However, they are indeed different, and these differences are based on the connection between them!

How to define

First, let's refer to the original English text to see the differences and connections in their definitions:

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.

Example:

scala> def m1(x:Int) = x+3
m1: (x: Int)Int    

scala> val f1 = (x: Int) => x+3
f1: Int => Int = <function1>

See? The definitions of methods and functions are already shown in the Scala parser signature. `def m1(x: Int) = x+3` is a simple method definition. In the signature, `m1: (x: Int)Int` indicates that method m1 has an Int parameter x, and its return value is Int.

`val f1 = (x: Int) => x+3` is a function definition. In the parser's signature, `f1: Int => Int = <function1>` indicates that function f1's method body accepts an Int parameter, and the output result type is Int.

From the above example, we can draw a conclusion:

A method is a logical operation block starting with `def` and having a parameter list (which may be absent), just like a member method in an object or class.

A function is an anonymous method (with or without a parameter list) assigned to a variable (or constant), and is an expression that uses the `=>` conversion symbol followed by a logical code block. The logical code block after the `=>` conversion symbol is written in the same way as the body of a method.

Other differences

A method can appear as part of an expression (calling a function and passing parameters), but a method (a method with parameters) cannot be a final expression. A method without parameters can, but this becomes a method call, because Scala allows omitting the parentheses when calling a no-argument method. A function can appear as a final expression.

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

scala> f1
res1: Int => Int = <function1>

A method can have no parameter list, or the parameter list can be empty. However, a function must have a parameter list (which can also be empty). A method name implies a method call, while a function name simply represents the function itself:

scala> def m2 = 100;
m2: Int

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

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

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

scala> m2
res2: Int = 100

scala> m3
res3: Int = 1000

scala> m3()
res4: Int = 1000

scala> f2
res5: () => Int = <function0>

scala> f2()
res6: Int = 100

Where a function is expected, we can provide a method.

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

Note:

Where a function is expected, we can use a method.

Where a function is not expected, a method will not be automatically converted to a function.

In Scala, operators are interpreted 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 add an underscore after the method name to force it into a function.

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_
<console>:10: error: not found: value m3_
       val f3 = m3_
                ^

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

scala> f3(3)
res0: Int = 27

Original URL: https://www.cnblogs.com/shihuc/p/5082701.html