[swift-evolution] [Pitch] Set of AnyObject by reference

张国晔 cc941201 at me.com
Sun Jul 17 04:00:21 CDT 2016


I'm building an document-based app, and I'm having a problem regarding to Set.

What I hope to achieve is to have a Set of unique objects by their references. For example, I might put in two rectangles with the same height and width (which makes them equal, but they are still different instances). Currently, I have two solutions:


Option 1:
 Inherit from NSObject

 class Rectangle: NSObject {
   var height, width: Int
   func isEqualByValue(_ other: Rectangle) -> Bool {
     return height == other.height && width == other.width
   }
 }

 Set<Rectangle> meets my requirements, but it drags a lot of baggage from Objective-C, also makes comparing by value ugly.


Option 2:
 Wrap them

 struct RectangleReference: Hashable {
   let value: Rectangle
   var hashValue: Int {
     return unsafeAddress(of: value).hashValue  // This might be removed in Swift 3 (SR-1957)
   }
 }
 func ==(lhs: RectangleReference, rhs: RectangleReference) -> Bool {
   return lhs.value === rhs.value
 }

 Set<RectangleReference> works fine, but it has unsafeAddress(of:) which is unsafe and might be removed in Swift 3.


What I'd like to use is a ReferenceSet<Element: AnyObject> which compares objects by their references. Any reference typed instances can be put in it without confirming to Hashable protocol. ReferenceDictionary<Key: AnyObject, Value> is also welcomed.

Any opinions?


- Guoye Zhang


More information about the swift-evolution mailing list