tags:

views:

213

answers:

1
val f1 = (a:Int) => a + 1
def f2 = (a:Int) => a + 1
def f3:(Int => Int) = a => a + 1

What's the difference?

scala> f1
res38: (Int) => Int = <function1>
scala> f2
res39: (Int) => Int = <function1>
scala> f3
res40: (Int) => Int = <function1>
+18  A: 

f1 is a function that takes an integer and returns an integer.

f2 is a method with zero arity that returns a function that takes an integer and returns an integer. (When you type f2 at REPL later, it becomes a call to the method f2.)

f3 is same as f2. You're just not employing type inference there.

missingfaktor
Nice and clear descriptions.
gpampara