I have a class that I’d rather not have based off of NSObject, but I do want to have identity based equality. I’ve done that as follows:
class Foobar { }
extension Foobar:Equatable { }
func == (a:Foobar, b:Foobar) -> Bool {
return a === b
}
What’s less clear to me is how to go about implementing an identity based hash
extension Foobar:Hashable {
var hashValue:Int {
// what magic should happen here?
}
}
ObjectIdentifier(self).hashValue is a decent approach; it hashes based upon the pointer address. That is how swift-corelibs-foundation does it for the swift implementation of NSObject: https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSObject.swift#L94
···
On Oct 5, 2016, at 9:53 AM, Travis Griggs via swift-users <swift-users@swift.org> wrote:
I have a class that I’d rather not have based off of NSObject, but I do want to have identity based equality. I’ve done that as follows:
class Foobar { }
extension Foobar:Equatable { }
func == (a:Foobar, b:Foobar) -> Bool {
return a === b
}
What’s less clear to me is how to go about implementing an identity based hash
extension Foobar:Hashable {
var hashValue:Int {
// what magic should happen here?
}
}
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users