Reputation: 361
From the API doc I found that Null
is a trait, but it says its sole instance is null
.
abstract final class Null extends AnyRef
Null is - together with scala.Nothing - at the bottom of the Scala type hierarchy.
Null is a subtype of all reference types; its only instance is the null reference. Since Null is not a subtype of value types, null is not a member of any such type. For instance, it is not possible to assign null to a variable of type scala.Int.
How is it possible to instantiate a trait? Any simple example to realize this concept would really helpful.
Upvotes: 0
Views: 244
Reputation: 55569
You cannot instantiate an instance of the class Null
, as it is abstract and final. Null
is a figment of the compiler similar Nothing
, used as a bottom-type for all reference types. That is, Null
is a sub-type of any other type that inherits from AnyRef
. Its only value is the null reference, but there is no way to instantiate the Null
class and magically get a null reference. It differs from Nothing
in that it is inhabited by a single value: null
.
Therefore, if you assign null
to an identifier, Null
will be inferred as its type if you don't hint otherwise.
scala> val a = null
a: Null = null
Upvotes: 2