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?
This is one of the few places where eta-expansion mechanism needs some help. Consider the simpler example:
There is a fundamental difference between
bar1andbar2. The former one is interpreted as callfooand assign value tobarwhile the latter: Change methodfoointo a function and assign it tobar2. As a resultbar1is just a simpleIntvariable whilebar2is actually a function that will call originalfoo()method (and print"foo").