I\'m attempting to create a simple generic node class that conforms to the Comparable protocol so that I can easily compare nodes without accessing their key. When I attempt
You're close! The Node class already specifies that for Node
, D
must conform to Comparable
. Therefore, Node
in the decl for ==
and <
is redundant. Instead, you want to restrict the types that the operators can be invoked upon:
func < (lhs: Node, rhs: Node) -> Bool {
return lhs.key < rhs.key
}
func == (lhs: Node, rhs: Node) -> Bool {
return lhs.key == rhs.key
}
class Node: Comparable {
var key: D!
var next: Node?
var prev: Node?
init(key: D) {
self.key = key
}
}