what does it mean assign “_” to a field in scala?

后端 未结 1 526
别跟我提以往
别跟我提以往 2021-01-17 18:46

I saw some scala code that assign \"_\" to a field of class, what does it mean ? Thanks

private var tk: TaggedKey = _
相关标签:
1条回答
  • 2021-01-17 19:16

    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 - if Tis Int or one of its subrange types,

    0L - if Tis Long,

    0.0f - if Tis Float,

    0.0d - if Tis Double,

    false - if Tis Boolean,

    () - if Tis Unit,

    null - for all other types T.

    0 讨论(0)
提交回复
热议问题