In Scala, what does "val a: A = _" (underline) mean?

What exactly does val a: A = _ mean val a: A = _ initialize the value? Is this a typical zero? Thank.

+84
initialization scala
Dec 01 '11 at 4:45
source share
2 answers

val a: A = _ - compilation error. For example:

 scala> val a: String = _ <console>:1: error: unbound placeholder parameter val a: String = _ ^ 

What works var a: A = _ (note var instead of val ). As Chuck says in his answer, this initializes the variable with a default value. From Scala Language Specification:

0 if T is Int or one of its subband types,
0L, if T is long,
0.0f if T Float,
0.0d if T is Double,
false if T is Boolean,
() if T is a unit,
null for all other types of T.

+103
Dec 01 '11 at 11:52
source share

Initializes a default value of type a . For example, the default value for Int is 0, and the default value for the reference type is null.

+26
Dec 01 2018-11-12T00:
source share



All Articles