Thank you for your compliment. I appreciate your feedback.
You are very right. For example, here is another interesting point related to Nothing
being the subclass of all classes. This sealed class
will not compile
sealed class LinkedList<T> {
data class Node<T>(val payload: T, var next: LinkedList<T> = EmptyList) : LinkedList<T>()
object EmptyList : LinkedList<Nothing>()
}
The out
variance modifier is missing. The compiler complains that the default value of next
cannot be set. We cannot write
var next: LinkedList<T> = EmptyList
unless we write
sealed class LinkedList<out T>