I saw some scala code that assign "_" to a field of class, what does it mean ? Thanks
private var tk: TaggedKey = _
Scala allows the use of underscores (denoted as '_') to be used as placeholders for one or more parameters. we can consider the underscore to something that needs to be filled in with a value. However, each parameter must appear only one time within the function literal.
case _ => does not check for the type, so it would match anything (similar to default in Java). case _ : ByteType matches only an instance of ByteType . It is the same like case x : ByteType , just without binding the casted matched object to a name x .
The multiple assignments can be possible in Scala in one line by using 'val' keyword with variable name separated by commas and the "=" sign with the value for the variable.
:: - adds an element at the beginning of a list and returns a list with the added element. ::: - concatenates two lists and returns the concatenated list.
It means: assign default value. Default value is defined as null
, 0
or false
depending on the target type.
It is described in 4.2 Variable Declarations and Definitions of the The Scala Language Specification:
A variable definition
var x : T = _
can appear only as a member of a template. It introduces a mutable field with type T and a default initial value. The default value depends on the type T as follows:
0
- ifT
is Int or one of its subrange types,
0L
- ifT
is Long,
0.0f
- ifT
is Float,
0.0d
- ifT
is Double,
false
- ifT
is Boolean,
()
- ifT
is Unit,
null
- for all other typesT
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With