[swift-evolution] Custom equality/hash for Sets

Maximilian Hünenberger m.huenenberger at me.com
Fri Feb 19 01:13:18 CST 2016


To me it seems like a future direction of the property behavior proposal.
There could be a behavior like "pointerEquatable" which is used as a transparent wrapper:

        bahavior pointerEquatable<Value: AnyObject> : Hashable {
                var value: Value

                var hashValue: Int {
                        // hash value from pointer
                }
                // omitting get/set
        }

        func == <T>(p1: pointerEquatable<T>, p2: pointerEquatable<T>) -> Bool {
                return p1.value === p2.value
        }

        // possible usage
        let set: Set<pointerEquatable<MyClass>> = [MyClass(), MyClass()]

This solution could then be used for any type not only Sets.

Best regards
- Maximilian

> Am 18.02.2016 um 23:58 schrieb Jacob Bandes-Storch via swift-evolution <swift-evolution at swift.org>:
> 
> Would it make sense for the standard library Set to provide variants (or parallel versions of the same data structure) that take custom hashValue/== implementations at init time (functions taking in Elements), rather than relying on Hashable/Comparable protocols?
> 
> Use case: I want a set of objects that are compared for equality using === rather than ==. This doesn't seem possible today, using Set, without creating some sort of wrapper object.
> 
> This particular case would be analogous to using NSHashTable with NSPointerFunctionsObjectPointerPersonality. (Maybe all I'm asking for is a Swiftier API for NSHashTable — including ArrayLiteralConvertible, using generics instead of UnsafePointer<Void>, etc.)
> 
> Similarly, C++'s unordered_map and friends have template parameters specifying the hash function and equality comparator, which use std::hash and == by default.
> 
> (Apologies if this has been discussed already; I haven't seen it.)
> Jacob
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160219/5ecf6eb3/attachment.html>


More information about the swift-evolution mailing list