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

Dmitri Gribenko gribozavr at gmail.com
Sun Jul 17 13:08:07 CDT 2016


On Sun, Jul 17, 2016 at 2:00 AM, 张国晔 via swift-evolution
<swift-evolution at swift.org> wrote:
> 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.

Try this:

public final class ReferenceEqualityBox<Wrapped : Equatable> : Hashable {
  public var value: Wrapped
  public init(_ value: Wrapped) {
    self.value = value
  }
  public var hashValue: Int {
    return ObjectIdentifier(self).hashValue
  }
}
public func == <Wrapped> (
  lhs: ReferenceEqualityBox<Wrapped>,
  rhs: ReferenceEqualityBox<Wrapped>
) -> Bool {
  return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
}

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-evolution mailing list