When implemented for a concrete type, my binary search tree node works fine in a playground:
class Node {
var data: Int
var leftChild: Node?
var rightChild: Node?
init(data: Int){
self.data = data
}
}
let n = Node(data: 42) // {data 42 nil nil}
When I try to make it generic, Xcode crashes and burns:
class GenericNode<T: Comparable> {
var data: T
var leftChild: GenericNode?
var rightChild: GenericNode?
init(data: T){
self.data = data
}
}
let g = GenericNode<Int>(data: 42)
When entered line-by-line, Xcode survives until I enter the actual assignment in init
. I tried specifying <AnyObject: Comparable>
, but apparently adding constraints to AnyObject
is not allowed. How does one make suitably constrained generic containers in Swift?