class LinkedNode<T> {
var value: T
var next: Self?
required init(_ value: T) {
self.value = value
}
}
class DoublyNode<T>: LinkedNode<T> {
weak var previous: DoublyNode?
override var next: Self? {
didSet { next.previous = self }
}
}
I wish it compiles like this. But it doesn't.
Stored property cannot have covariant 'Self' type
I have to rewrite the code to make it compile and work:
class LinkedNode<T> {
var value: T
var next: LinkedNode?
required init(_ value: T) {
self.value = value
}
}
class DoublyNode<T>: LinkedNode<T> {
weak var previous: DoublyNode?
override var next: LinkedNode<T>? {
didSet { (next as! Self).previous = self }
}
}
And everywhere in the later code every time after referring to next property I have to manually cast it to DoublyNode (when I'm working with DoublyNode) because it always has type LinkedNode.
It's manageable but annoying and ugly.