0

What are the practical differences between a function and a method in Scala? I am only trying to build a list of practical differences, which I didn't readily find in the other entry talking about functions and methods in Scala.

So far, I have found that:

  1. functions cannot have default values for their parameters but methods can,
  2. functions can be anonymous, but methods always have a name,
  3. methods can be type-parameterized, but functions can't.

Any other real difference?

Also, what is going on here? The types of both objects seem to be the same?

scala> val f = (x: Int) => x + 1
f: Int => Int = $$Lambda$1097/560897187@75120e58

scala> :t f
Int => Int

scala> def g = (x: Int) => x + 1
g: Int => Int

scala> :t g
Int => Int
Frank
  • 4,341
  • 8
  • 41
  • 57
  • What you have there is two functions, not a function and a method – puhlen Jul 28 '17 at 15:46
  • @puhlen - I thought `def` was the marker of methods - is that not the case? – Frank Jul 28 '17 at 15:48
  • @ggovan - I created this entry after seeing the one you reference because I felt the other entry was not all that clear from a practical point of view. – Frank Jul 28 '17 at 15:48
  • @Frank more specifically, you have a method that takes no arguments returns a function that takes an int and returns and int. If you just wanted to implement it as a method that takes an int and returns an it it would look like `def h(x: Int) = x + 1` – puhlen Jul 28 '17 at 15:49
  • @puhlen - got it. Thanks. – Frank Jul 28 '17 at 15:51
  • You can add the difference in `return` behavior to your list, as described in one of the answers to the referenced post: https://stackoverflow.com/a/2529905/5344058; Altogether, this still seems like a duplicate to me. – Tzach Zohar Jul 28 '17 at 16:08

0 Answers0