Assign an underscore to a variable. What does underline do?

I recently came across the following code:

var myVariable: variableKind = _

This seems to be a way to appropriate null myVariable.

Can someone explain the rationale _in this case? What is the difference between assigning a variable _and a nullvariable?

+4
source share
2 answers

It initializes the variable with a default value - this value depends on the type. For numeric types, this is zero, falsefor booleans, ()for Unitand null for types that extend AnyRef.

+5
source

. "variableKind" AnyRef, ( ) null. ..

+2

Source: https://habr.com/ru/post/1531438/


All Articles