Consider this code:
var unit: Unit = null
unit: Unit = ()
a) Why am I allowed to assign null to a value class? (see §12.2.3)
b) Why does the null get converted to ()?
From the scala specification section 6.26.1:
Value Discarding. If
ehas some value type and the expected type isUnit,eis converted to the expected type by embedding it in the term{ e ; () }.
In other words, your code is equivalent to
var unit: Unit = {null; ()}
unit: Unit = ()
The null isn't converted -- it's merely ignored and replaced by (), the predefined Unit value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With