The Differences Between Scala Methods and Functions
Category Programming Techniques
Before I learned Scala, I mainly used Java and Python for my daily work development. In my impression, there was no special deliberate distinction between methods and functions, just as we do not deliberately distinguish between mass and weight in our daily lives. However, there are indeed differences between them, and these differences are also based on the connections between them!
How to Define
First of all, 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* . That means there is* 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>
Other Differences
A method can appear as part of an expression (calling a function and passing arguments), but a method (a method with parameters) cannot be the final expression (a method without parameters can, but this becomes a method call because Scala allows the omission of parentheses when calling a method without parameters), while a function can appear as the 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, and the parameter list can also be empty. However, a function must have a parameter list (which can also be empty). The method name implies a method call, while the function name only 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
We can provide a method where a function is expected.
This is because if a method is provided where a function is expected, the method will automatically be converted into 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 does not automatically convert into a function.
In Scala, operators are interpreted as methods:
Prefix operators: op obj is interpreted as obj.op
Infix operators: obj1 op obj2 is interpreted as obj1.op(obj2)
Postfix operators: 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)
We can add an underscore after the method name to force it to become a function.
Note: There should be at least one space between the method name and the underscore!
``` scala> def