I am working on building a flexible data structure in Swift called Node that, by itself, is not tied to any type of content. However, the Payload data within the Node is declared as the following generic Element struct that conforms to the Equatable protocol:
public struct Element<T>: Equatable {
var data: T;
}
public func ==<T:Equatable>(lhs: Element<T>, rhs: Element<T>) -> Bool {
return lhs.data == rhs.data;
}
The constraint is that the Element has to be tied to an equatable class type. The problem I'm having is with the Node containing this Element. The Node would be used in a Dictionary, Array, or any other container type. Here is what I have:
public class Node: Equatable {
var payload: Element<AnyObject>
init(_data: Element<AnyObject>) {
self.payload = _data
}
}
public func ==(lhs: Node, rhs: Node) -> Bool {
return lhs.payload == rhs.payload;
}
Of course, I'm getting the error that AnyObject does not conform to Equatable. Is there any way to constrain the declaration of the payload to only Objects that are equatable? At this point, I don't know what kind of objects may be stored in the payload.
Also just realized I need to put a class check in the == function in Node to make sure the two Node payloads are compatible to be compared--don't need them to be.
Thoughts? Thank you!
==
implementation refers tolhs.data
andrhs.data
wherelhs
andrhs
are Nodes. But your Node class has nodata
property, so I don't see what that's supposed to mean. Did you meanlhs.payload
andrhs.payload
? – matt