I have the following Scala code:
class X[T1 <: AnyRef] {
var _x : T1 = null
}
The code _x = null is highlighted as error:
error: type mismatch;
found : Null(null)
required: T1
var _x : T1 = null : T1
If I add Null type constraint everything works fine. Why this happens? Scala defines AnyRef as equivalent of java.lang.Object, which is of course, nullable.
Instead of
use
Explanation from the Scala Language Specification: