赋值“_"是什么意思?到 Scala 中的一个字段? [英] what does it mean assign "_" to a field in scala?

查看:41
本文介绍了赋值“_"是什么意思?到 Scala 中的一个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些将_"分配给类字段的 Scala 代码,这是什么意思?谢谢

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

private var tk: TaggedKey = _

推荐答案

意思是:分配默认值.默认值根据目标类型定义为 null0false.

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

0L - 如果 T 为 Long,

0L - if Tis Long,

0.0f - 如果 T 是 Fl​​oat,

0.0f - if Tis Float,

0.0d - 如果 T 是 Double,

0.0d - if Tis Double,

false - 如果 T 是布尔值,

false - if Tis Boolean,

() - 如果 T 是单位,

() - if Tis Unit,

null - 对于所有其他类型 T.

null - for all other types T.

这篇关于赋值“_"是什么意思?到 Scala 中的一个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆