Skip to content

Null type variables can be assigned to value types #11299

Closed
@kynthus

Description

@kynthus

Compiler version

3.0.0-M3

Minimized example

val myNull: Null = null

val n1: Int = null   // error
val n2: Int = myNull // ok `val n2: Int = Integer2int(myNull)`

Output

Initialization with null literals will result in an error, but initialization with null type variables is allowed.

scala> val n1: Int = null
1 |val n1: Int = null
  |              ^^^^
  |              Found:    Null
  |              Required: Int

scala> val n2: Int = myNull
val n2: Int = 0

Expectation

I expected it to be an error, because null literals are not allowed.
Implicit conversion that converts Java wrapper class to value type seems to work.
It is converted to the default value of each value type by asInstanceOf.
Is this the expected behavior?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions