scala - What's the purpose of null in X >: Null <: Y? -
for example:
abstract type classsymbol >: null <: universe.typesymbol universe.classsymbolapi
this found here. realize there description of does, can't make sense of syntax standpoint.
what's purpose of null? looks might kind of default surmise because see kind of syntax types, , think null can returns type without program breaking. abstract type, doesn't have parameter can specify later, how work abstract type?
null
type specifies can null
. it's not supertype, strictly speaking, because can't anything null
except pass around , check that, yep, it's still null
. in way it's nothing
, except can't instance of nothing
pass around.
now, 1 say, "well, anyref
null
", how think of instance null
itself? null
can fill in pretty anything, which, type, means must subtype of can null
! , that's null
is: subtype of can null
.
wait, wait, say. can't actually have subtype of everything--that means null
have every possible functionality! if think it, null
does promise every type of functionality anyref
has--but they're empty promises, , whenever try use functionality nullpointerexception
instead.
now, question is, why make type >: null
(meaning null
valid value it) instead of <: anyref
should imply same thing?
well, turns out scala doesn't model things way, leave open possibility have anyref
absolutely sure not ever possibly null
. asking <: anyref
("i subclass of anyref") doesn't mean same thing theoretically, though in practice absolutely-not-null-anyref
s not implemented. (also, any
awkward if null
not explicit type.)
so type foo >: null <: bar
means explicitly admit null
can fill in instances of possible type of foo
, , can write val foo: foo = null
.
Comments
Post a Comment