Reputation: 53786
Output of below :
getNum(_);
getNum(3);
def getNum(num: Int) {
println("Num is " + num)
}
is
Num is 3
Why is getNum(_);
not invoked ? How is _
used in this case ?
Upvotes: 0
Views: 44
Reputation: 12783
What you'd expect it to be? getNum(null)
?
The getNum(_);
is translated into, something like:
{ x:Int => getNum(x) }
Which is a anonymous function and a value itself.
You could do for example:
val f = getNum(_)
f(42)
Then you'd see:
Num is 42
Upvotes: 6
Reputation: 3150
_
is used to partially apply a function. Partial application of a function produces another function with some of its parameters already applied.
val f = getNum(_) // partially apply
f(3) // apply the function
Upvotes: 1