Reputation: 2882
I am trying to learn scala. In one of his lectures, when Martin Odersky talks about Function objects, he talks about how scala functions are expanded to a AnonFun
class that implements FunctionN
(where 1<=N<=22) trait with an apply
method. As an example, he explains that the Anonymous function (x: Int) => x * x is
gets expanded as the following class
new Function1[Int, Int] {
def apply(x: Int) = x * x
}
new AnonFun
So my question is, why does Function1
take generic type [Int, Int]
. Shouldn't one suffice?
Upvotes: 0
Views: 54
Reputation: 57639
The last type defines the type of the return value of your function. See this tutorial for further examples that illustrate this better.
Excerpt from the tutorial:
Int => Int
(Int, Int) => String
() => String
results in these function object types:
Function1[Int, Int]
Function2[Int, Int, String]
Function0[String]
Upvotes: 4