I am learning from this site http://naildrivin5.com/scalatour/wiki_pages/ExplcitlyTypedSelfReferences/
trait BaseComponent {
protected var label:Label = _
}
In this case what does the placeholder _ stands for?What would be the alternative?
I am learning from this site http://naildrivin5.com/scalatour/wiki_pages/ExplcitlyTypedSelfReferences/
trait BaseComponent {
protected var label:Label = _
}
In this case what does the placeholder _ stands for?What would be the alternative?
The placeholder syntax for a variable assings the default value to the variable. Assuming Label
inherits AnyRef
, that would be null
.
The Scala language specification lays this out explicitly:
4.2 Variable Declarations and Definitions:
A variable definition
var x: T = _
can appear only as a member of a template. It introduces a mutable field with typeT
and a default initial value. The default value depends on the typeT
as follows:
| default | type T |
|---------|----------------------------------|
| 0 | Int or one of its subrange types |
| 0L | Long |
| 0.0f | Float |
| 0.0d | Double |
| false | Boolean |
| () | Unit |
| null | all other types |