How about this:
val double = (_: Int) * 2
Note Here double
is a Function
rather than a method
. In your first example, you have defined a method
named double
with return type of Function
. In your second example, your just have defined a method
.
Function
is different from method
in Scala.
In case the compiler can get the type information, we can write the Function
even simple:
scala> def apply(n: Int, f: Int => Int) = f(n)
apply: (n: Int, f: Int => Int)Int
scala> apply(10, 2*)
res1: Int = 20
scala> apply(10, 100+)
res2: Int = 110