12

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

private var tk: TaggedKey = _
1

1 Answer 1

24

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.