
- The function type A => B is just an abbreviation for the class scala. Function1[A, B], which is defined as follows.
1 2 3 4 5 6 7 |
package scala; trait Function1[A, B] { def apply(x: A): B } |
- An anonymous function such as
1 |
(x: Int) => x * x |
is expanded to:
1 2 3 4 5 |
new Function1[Int, Int] { def apply(x: Int) = x * x } |
- A function call, such as f(a), where f is a value of some class type, is expanded to f.apply(a)
1 2 3 |
val f = (x: Int) => x * x; f(7) |
would be
1 2 3 4 5 6 7 |
val f = new Function1[Int, Int] { def apply(x: Int) = x * x } f.apply(7) |
- A method such as
1 |
def f(x: Int): Boolean = ... |
is not itself a function value.
But if f is used in a place where a Function type is expected, it is converted automatically to the function value
1 |
(x: Int) => f(x) |
Leave a Reply