I am trying to figure out how to use algebraic data types in Kotlin, so I\'m trying to implement a basic BinaryTree type the following way.
sealed class Tree<
First you need to make T
an out
parameter. Then you can use Nothing
as a type argument for Empty
.
sealed class Tree<out T>{
class Node<T>(val left: Tree<T>, val right: Tree<T>): Tree<T>()
class Leaf<T>(val value: T): Tree<T>()
object Empty: Tree<Nothing>()
}
Nothing
is a special type in Kotlin, which cannot have an instance and is a subtype of all other types. So I would say it's opposite to Any
in Kotlin type hierarchy.