Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused by "...extends Function1[Int, Int]"

Tags:

scala

In the twitter Scala School "Basics Continued" http://twitter.github.io/scala_school/basics2.html there is the following Object definintion

object addOne extends Function1[Int, Int] {
  def apply(m: Int): Int = m + 1
}

I don't fully understand the [Int, Int] type parameterization. When we extends Function1, I believe I am declaring that I will have an apply that takes a single argument, Why the 2nd Int in Function1[Int, Int] when my apply is built to only take a single Int argument?

Please explain.

like image 531
Bob Kuhar Avatar asked Jun 10 '13 21:06

Bob Kuhar


1 Answers

It's return type. Function1[Int, Int] is the same as Int => Int. It takes one Int and returns one Int.

We can write simplified scala.Function1 like this:

trait Function1[T1, R]{
  def apply(v1: T1): R
}
like image 194
Infinity Avatar answered Nov 06 '22 09:11

Infinity