Why does val x: Int = _
not compile but var x: Int = _
does?
I\'m getting error: unbound placeholder parameter
.
You can't reassign a vals value, so you cannot use _ (init with a default value for prescribed type) with it. But you can reassign a vars value, so it's a logical to initialize it with some default value, like in Java
In this context, _
means "I will initialize this later, just fill in whatever the sensible default is in the meantime". Since you can't reassign a val
, this doesn't make sense.
For the same functionality--to get the sensible default--for a val
, you can use
val x: Int = null.asInstanceOf[Int]