0

Possible Duplicate:
What are all the uses of an underscore in Scala?

When I follow a particular tutorial, I happen to see that the following two usages gives the same result. I understand the first one, but I do not understand why the 2nd one also works. Can someone give me an explanation, and at the same time give a summary of _ usage?

def sum (a:Int, b:Int) = a + b

val sumAsFunction1 = sum(_:Int, _:Int)  
// I understand this, _ used as placeholder of parameters

val sumAsFunction2 = sum _
// why this usage has the same effect as sumAsFunction1?
Community
  • 1
  • 1
chen
  • 4,302
  • 6
  • 41
  • 70
  • I agree this is a duplicate of that question, but the answers are valuable to newbies. Can we somehow merge these two questions for other newbie's benefit? – chen May 26 '12 at 18:53
  • although this answer will be possible closed as duplicate (actually, only last question is duplicate) and no new answers will be placed we will persist answers already written, so future visitors can view and benifit from them – om-nom-nom May 27 '12 at 02:54

2 Answers2

1

This is one of the few places where eta-expansion mechanism needs some help. Consider the simpler example:

def foo() = {
  println("foo"); 
  42
}

val bar1 = foo
val bar2 = foo _

There is a fundamental difference between bar1 and bar2. The former one is interpreted as call foo and assign value to bar while the latter: Change method foo into a function and assign it to bar2. As a result bar1 is just a simple Int variable while bar2 is actually a function that will call original foo() method (and print "foo").

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
1

The underscore is used for a lot of things, but in this situation, it's used to denote that you want the un-called version of the sum function.

You define sum(a: Int, b:Int) = a + b, so think of sum _ as an anonymous function that takes the two arguments and returns their sum. You can pass the function around, since it's an instance of Function2[Int,Int,Int].

Dylan
  • 13,645
  • 3
  • 40
  • 67
  • "think of sum _ as an anonymous function" -- this is so helpful to my understanding, thanks. – chen May 26 '12 at 18:55
  • `sum` is not a function, it is a method. If it were a function, you wouldn't need `_` to turn it into one. – Jörg W Mittag May 27 '12 at 02:50
  • Ahh I had never noticed the technical difference between function and method. This (http://jim-mcbeath.blogspot.com/2009/05/scala-functions-vs-methods.html) clears it up. – Dylan May 29 '12 at 13:29
  • @Dylan: Unfortunately, the Scala community tends to be rather loose in the usage of the word "*function*". They use it to mean both "*function*" in the Scala sense and "*subroutine*" in the general programming sense. Even the Scala Language Specification, while in some parts carefully explaining the differences between methods and functions, in other parts refers to methods as "*functions*" (in the "*subroutine*" sense). This is okay in informal conversations among experienced Scalarazzi, where every participant understands the differences, but on a Q&A site precise terminology is important. – Jörg W Mittag Jun 03 '12 at 10:59