Reputation: 4481
I searched a lot, but I didn't find anything, What is the meaning of this type of type declaration
in Scala?
type Ident >: Null <: AnyRef
Upvotes: 9
Views: 1885
Reputation: 21557
In your case Null
is a subtype of Ident
and AnyRef
is its supertype. In without this boundaries common subtype of all type is Nothing
, but you specify it to be Null
, with is a subtype of all object when Nothing is a subtype of everything (including Int, Long, etc..)
Things like Null and Nothing are used in Type System, basically in type (Contra/Co)variance. Example:
sealed trait Container[+A >: Null <: AnyRef]
case class Full[A >: Null <: AnyRef](value: A) extends Container[A]
case object Empty extends Container[Null]
This compiles fine:
val c: Container[String] = Full("String")
val e: Container[String] = Empty
But this fails, because our lower bound is Null and upper is AnyRef:
val ff: Container[Int] = Full(10)
val f: Container[Int] = Empty
We can't put Int
here beacuse this type violates out contraints
Upvotes: 1
Reputation: 38045
Keyword type
is for type alias declaration, just like val
and def
are for value and method declaration. In this case it's an abstract type alias with constraints, so it's a type member of some trait
or class
- type alias in local scope can't be abstract and can't have constraints.
Type Ident
is a subtype of AnyRef
and supertype of Null
.
AnyRef
AnyRef
is an ancestor of all reference types, all types except Int
, Long
, Char
and so on (Java primitives).
Null
Null
is subtype of all "nullable" types. In fact it's a subtype of all reference types.
Since all AnyRef
are nullable the only additional constraint from >: Null
is that Ident
is not Nothing
.
Upvotes: 16
Reputation: 170765
To add to @senia's answer: this isn't a type declaration, this is an abstract type member declaration, found in context like
trait Foo { // or perhaps class
type Ident >: Null <: AnyRef
}
and means that any concrete implementation of Foo
must define some type to be Ident
, and this type has to satisfy constraints described in @senia's answer.
Upvotes: 7