title: Set of weak observers in Swift
body: <p>I am trying to implement a structure that allows me to store a set of weak observers. </p>
  
  <p>Here is the observer wrapper: </p>
  
  <pre><code>public func ==&lt;T: Hashable&gt;(lhs: WeakObserver&lt;T&gt;, rhs: WeakObserver&lt;T&gt;) -&gt; Bool {
    return lhs.hashValue == rhs.hashValue
  }
  
  public struct WeakObserver&lt;T where T: AnyObject, T: Hashable&gt; : Hashable {
  
    private weak var weakObserver : T?
  
    public init(weakObserver: T){
      self.weakObserver = weakObserver
    }
  
    public var hashValue : Int {
      return self.weakObserver!.hashValue
    }
  
  }
  </code></pre>
  
  <p>And here is the protocol every observer needs to conform to: </p>
  
  <pre><code>public protocol DataModelObserverProtocol : class, Hashable, AnyObject {
  
    func someFunc()
  
  }
  </code></pre>
  
  <p>Usage: </p>
  
  <pre><code>public class DataModel: NSObject, DataModelInterface {
  
    public var observers = Set&lt;WeakObserver&lt;DataModelObserverProtocol&gt;&gt;()
    //^^^^^ Using 'DataModelObserverProtocol' as a concrete type conforming to protocol 'AnyObject' is not supported
  }
  </code></pre>
  
  <p>Now, while I am aware that this might be a limitation by Swift itself, I am looking for an alternative solution without having a concrete class as type constraint (if that's not possible, which I am afraid is the case, I'd still love to get alternative "non-hacky" solutions).</p>