I'm not sure how you do equality checks, but I'm assuming it's done using some same type commonality that AnyDataType
provides. As such type erasure might be able to help in your situation. Explanation using inline comments.
protocol AnyDataType : Equatable {
// Your requirments here
func foo() -> Int
}
extension AnyDataType {
static func ==(lhs: Self, rhs: Self) -> Bool {
return lhs.foo() == rhs.foo()
}
}
final class AnyData<T> : AnyDataType {
// All requirments would need be done here in a similar fashion
var _foo: () -> Int
init<U: AnyDataType>(_ store: U) {
// Assign all methods from store to closures you declared above
_foo = store.foo
}
// fulfill protocol requirments
func foo() -> Int {
return _foo()
}
static func ==(lhs: AnyData<T>, rhs: AnyData<T>) -> Bool {
return lhs.foo() == rhs.foo()
}
}
// Some concrete conforming types
struct A : AnyDataType {
func foo() -> Int {
return 1
}
}
struct B : AnyDataType {
func foo() -> Int {
return 2
}
}
// Quick test
let a = AnyData<Int>(A())
let b = AnyData<Int>(B())
let x = [[a, b], [b, a]]
let y = [[b, a], [b, a]]
print(x == y)