This:
abstract class Reference[T <: AnyRef](var referent: T){
def get: T = referent
def clear: Unit = referent = null
}
Fails with
[error] type mismatch;
[error] found : Null(null)
[error] required: T
[error] def clear: Unit = referent = null
This works though:
abstract class Reference[T >: Null <: AnyRef](var referent: T){
def get: T = referent
def clear: Unit = referent = null
}
Can someone explain what's going on here? If I have already bound T to be an AnyRef, how could it be non-nullable? Furthermore, why must I bound T to AnyRef in the first place? Aren't generics erased unless i @specialize them?
Source: I'm trying to translate this code to ScalaJS from Java, where everything works fine without any weird type bound trickery.