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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With