user3058661
user3058661

Reputation: 23

primary constructor, values with underscore

A try this (in Scala 2.10.3) :

scala> class A(var a: Int = _)
<console>:1: error: unbound placeholder parameter
       class A(var a: Int = _)
                            ^

What is the problem?, the underscore for default value-type can not be used in a primary constructor?

Upvotes: 2

Views: 762

Answers (1)

Revan
Revan

Reputation: 261

The underscore is used for placeholder of the default value of the type (see Scala language specification). I am not really sure why you would want to do this type of initialization instead of: class A(var a: Int = 0) as it is likely to be more concise and other coders will know immediately the default value.

Upvotes: 2

Related Questions