Scala defines a type called Nothing
that is a sub-type of all types. Is it possible to define our own "bottom types" that should be at the bottom of an inheritance tree that extends from a given super-type?
For example, suppose I have some type Foo
, and I want to say Xyzzy
should be at the bottom of Foo
's inheritance tree - in other words, it should inherit from anything that inherits from Foo. If we introduce a new type Bar
, and Bar <: Foo
, then Xyzzy <: Bar
. I'm basically looking for a way to say class Xyzzy isBottom Foo { ... }
. Is there any way to achieve this directly, or indirectly achieve this effect?
It is possible to come close to the idea you're looking to do. This is not a general "bottom" type for all types in an inheritance chain. Instead, it's a bottom type specific to traits that use generics. You need to reuse Nothing
in the type definition within the inheritance structure. Take a look at List[_]
to see how they've defined Nil
:
case class ::[A](value: A, next: List[A]) extends List[A]
case object Nil extends List[Nothing]
Hence, Nil
is a List[A]
for all A
because the type parameter of list is defined as List[+A]
.