mon
mon

Reputation: 22244

Explanation of "unbound placeholder parameter"

Question

Why case 2 causes "unbound placeholder parameter" while case 1 is OK?

Case 1

val h: (Int => Int) = (x:Int) => { scala.util.Random.nextInt(x) }
val _h: (Int => Int) = { scala.util.Random.nextInt(_) }
h(5)     // 3
_h(5)    // 3

Case 2

val f: (Int => Int) = (x:Int) => { x }
f(5)     // 5
// Causes error
val _f: (Int => Int) = { _ }   <----- unbound placeholder parameter

Upvotes: 3

Views: 5076

Answers (2)

jwvh
jwvh

Reputation: 51271

.nextInt(_) is not an example of placeholder usage. In this case the underscore _ is used for "eta expansion" of the nextInt() method, i.e. promoting the method to a proper function. The result is equivalent to the following:

val _h: (Int => Int) = util.Random.nextInt

To get your _f() code to work you could do this.

val _f: (Int => Int) = identity

Upvotes: 3

Oleg Pyzhcov
Oleg Pyzhcov

Reputation: 7353

_ is never interpreted as x => x in Scala.

If I understand the part of language spec where it talks about "binding underscore sections" correctly, this is precisely because Scala reserves _ for partial application. Like in case of scala.util.Random.nextInt(_) it means x => nextInt(x) and not nextInt(x => x).

Upvotes: 4

Related Questions