4

What is the difference between

def plusOne(n: Int) = n + 1

and

val plusOne = (n : Int) => n + 1
Squidly
  • 2,707
  • 19
  • 43
  • 3
    http://jim-mcbeath.blogspot.co.uk/2009/05/scala-functions-vs-methods.html – Luigi Plinge Aug 03 '12 at 16:18
  • possible duplicate of [Difference between method and function in Scala](http://stackoverflow.com/questions/2529184/difference-between-method-and-function-in-scala) – 0__ Aug 03 '12 at 20:38
  • @0__: Not a dupe. This is about differences between functions and *`var`s*. – Mechanical snail Aug 06 '12 at 05:07
  • @Mechanical snail - I see no `var`s. It is method versus function clearly (`val plusOne` holds a function which is equivalent to `plusOne _` defined as method). – 0__ Aug 06 '12 at 15:23
  • If you don't know the terminology, then the question 'what is the difference between a function and a lambda'd val' becomes irrelevant to you. If I'd know that was what I was asking, I wouldn't have need to ask this question. – Squidly Aug 06 '12 at 16:40

2 Answers2

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.

Community
  • 1
  • 1
Owen
  • 38,836
  • 14
  • 95
  • 125
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

hiltym
  • 146
  • 3