I'm trying to understand the crucial difference between these two approaches of referencing / defining Function Literal
(reference to anonymous function
):
By val
scala> val v2 = new Function[Int, Int] {
| def apply(a: Int): Int = a + 1
| }
v2: Int => Int = <function1>
And by def
scala> def f2 = new Function[Int, Int] {
| def apply(a: Int): Int = a + 1
| }
f2: Int => Int
It seems that it pretty much the same in terms of use. I either can pass v2
or f2
to the function that accepts (Int) => Int
as an argument. Passing arguments to its..
I guess or the case of v2 it creates an Function1
object that refers to the Function1
object.. like a proxy
?
Ok.. My question is: what is advantage and disadvantages of 1th and 2nd approach?
And of it is defined by def
, is it still Function Literal
?