赋值“_"是什么意思?到 Scala 中的一个字段? [英] what does it mean assign "_" to a field in scala?
问题描述
我看到一些将_"分配给类字段的 Scala 代码,这是什么意思?谢谢
I saw some scala code that assign "_" to a field of class, what does it mean ? Thanks
private var tk: TaggedKey = _
推荐答案
意思是:分配默认值.默认值根据目标类型定义为 null
、0
或 false
.
It means: assign default value. Default value is defined as null
, 0
or false
depending on the target type.
它在 Scala 语言规范:
It is described in 4.2 Variable Declarations and Definitions of the The Scala Language Specification:
变量定义 var x : T = _
只能作为模板的成员出现.它引入一个类型为 T 和默认初始值的可变字段.默认值取决于类型 T 如下:
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
- 如果 T
是 Int 或其子范围类型之一,
0
- if T
is Int or one of its subrange types,
0L
- 如果 T
为 Long,
0L
- if T
is Long,
0.0f
- 如果 T
是 Float,
0.0f
- if T
is Float,
0.0d
- 如果 T
是 Double,
0.0d
- if T
is Double,
false
- 如果 T
是布尔值,
false
- if T
is Boolean,
()
- 如果 T
是单位,
()
- if T
is Unit,
null
- 对于所有其他类型 T
.
null
- for all other types T
.
这篇关于赋值“_"是什么意思?到 Scala 中的一个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!