Look at the following method
def toOption[T <: AnyRef](value: T | Null): Option[T] =
value match {
case null => None
case content: T => Some(content)
}
If you inspect the content
pattern variable, the type is T | T & Null
, which I guess is the result of the the intersection of the type of the scrutinee value: T | Null
and the type of the matched pattern element: T
.
Shouldn’t the type T & Null
be simplified to Null
by the compiler when T<:AnyRef
? What is the difference between the types Null
and T & Null
?
If the definition of the intersection A & B
was that it represents values that are members of both A
and B
then:
- if the values represented by
T
includednull
then the values that are members of bothT
andNull
would be only thenull
value; - if the values represented by
T
did not include thenull
then the values that are members of bothT
andNull
would be none (Nothing
) - List item
Therefore T & Null
= Null | Nothing
= Null
If the definition of the intersection A & B
was that a value conforms to A & B
implies that it conforms to both A
and B
then:
- if
null
conforms toT
then onlynull
would conform to bothT
andNull
; - if
null
does not conform toT
then no value would conform to bothT
andNull
. - List item
Therefore T & Null
= Null | Nothing
= Null
Am I missing something or the compiler has a bug?