3

What is the difference between

def plusOne(n: Int) = n + 1

and

val plusOne = (n : Int) => n + 1
4

2 回答 2

4

What the difference really comes down to is that the first is a "method", and the second is a "function", and in Scala these two things are surprisingly different.

You could see, for example, Difference between method and function in Scala.

于 2012-08-03T16:20:07.100 回答
4

Actually, both of them are functions.

The first one is a method or a local function, depending on where it is declared. The second one is a function value, which is an object instantiated at runtime. Methods, local functions, function values, and function literals are all flavors of functions in Scala.

See here for a chapter of Martin Odersky's book on this topic: http://www.artima.com/pins1ed/functions-and-closures.html

于 2012-08-04T08:43:59.120 回答