0

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?

1
  • 2
    One for the input type, and one for the return type Commented Apr 27, 2017 at 23:27

1 Answer 1

4

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.