I have the following Scala code:
class X[T1 <: AnyRef] {
var _x : T1 = null
}
Code _x = null is highlighted as an error:
error: type mismatch;
found : Null(null)
required: T1
var _x : T1 = null : T1
If I add a constraint of type Null, everything will work fine. Why is this happening? Scala defines AnyRef as the equivalent of java.lang.Object, which of course is nullable.
source
share