I have some class that i want to put into a dictionary however that class is does not conform to Hashable i cant use it as a key in a Swift dictionary. Since its a class it can be identified by its location in memory and im happy to use that its identifier, the type itself doesnt fall into the value semantics world anyway.
Therefore i declare an extension to make it so
extension SomeGenericType : Hashable {
public var hashValue: Int {
return unsafeAddressOf(self).hashValue
}
}
This seems ok, however Hashable inherhits from Equatable so i need to implement tha too, my first try:
public func ==(lhs: SomeGenericType, rhs: SomeGenericType) -> Bool {
return unsafeAddressOf(lhs) == unsafeAddressOf(rhs)
}
Errors with
"Reference to generic type 'SomeGenericType' requires arguments in <...>"
...fair enough so lets do that
public func ==<T : SomeGenericType >(lhs: T, rhs: T) -> Bool {
return unsafeAddressOf(lhs) == unsafeAddressOf(rhs)
}
Now it says
"Reference to generic type 'SomeGenericType' requires arguments in <...>"
Hmm so i can make this work for all SomeGenericType's regardless of what type it gets. Maybe we can just put AnyObject in there?
public func ==<T : SomeGenericType<AnyObject>>(lhs: T, rhs: T) -> Bool {
return unsafeAddressOf(lhs) == unsafeAddressOf(rhs)
}
Ok now == is happy but apparantly im not implementing Hashable properly as there is now a error on my hashable extension saying:
"Type 'SomeGenericType<T>' does not conform to protocol 'Equatable'"
Ive tried fiddling with a constrained Extension on SomeGenericType but i cant seem to make a constrained extension of a type adopt another protocol, the language grammar doesnt seem to allow it, so Im in a bit of pickle here
Edit, for reference SomeGenericType is defined as follows:
class SomeGenericType<T> {
}