title: Swift Generalized Existentials
body: <p>Redundant introduction aside, I want to have something like this:</p>
  
  <pre><code>let collection : Any&lt;Sequence where .Iterator.Element == String&gt; = ...
  </code></pre>
  
  <p>or</p>
  
  <pre><code>let collection : Sequence&lt;where .Iterator.Element == String&gt; = ...
  </code></pre>
  
  <p>This is referred to as "Generalized existentials" in <a href="https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#generalized-existentials" rel="noreferrer">Apple's Generics Manifesto</a>. (I think) I really need this for a number of use cases and this:</p>
  
  <blockquote>
    <p>protocol 'P' can only be used as a generic constraint because
    it has Self or associated type requirements.</p>
  </blockquote>
  
  <p>makes "the first Protocol Oriented Language" be cumbersome for me to understand. Lack of this makes me fight Swift's type system and create adverse generic "abstract" classes where there should be a protocol with <code>associatedtype</code>.</p>
  
  <p>Here's one example, that hit me the most, delegate for a generic class:</p>
  
  <pre><code>protocol GenericClassDelegate : class {
      associatedtype ItemType
      func didDoThat(who : GenericClass&lt;ItemType&gt;) 
  }
  
  class GenericClass&lt;T&gt; {
      weak var delegate : GenericClassDelegate&lt;where .ItemType == T&gt;? // can't do that
  
      func notify() {
          delegate?.didDoThat(who: self)
      }
  }
  </code></pre>
  
  <p>While I can describe <code>GenericClassDelegate</code> protocol, I (currently in Swift 3) can't have a variable or constant of that type (or any type conforming to restrictions).</p>
  
  <p>Don't confuse this question with <a href="https://stackoverflow.com/q/27725803/1032151">How to use generic protocol as a variable type</a> or <a href="https://stackoverflow.com/q/28213966/1032151">Swift delegate protocol for generic class</a>, as <strong>my questions are:</strong></p>
  
  <ol>
  <li>Are there any proposals or discussions currently present on introducing Generalized Existentials into Swift, what are the plans? If no, how can I participate and affect this?</li>
  <li>If Swift was designed that way (with Associated Types, but without Generalized Existentials), maybe it implies some architectural shift. What am I expected to replace delegation pattern with?</li>
  </ol>
  
  <p>P.S. Don't suggest thunks with type erasure when you capture delegate's function in a closure, that is so much wrong and misleading, I'd even call it a crutch.</p>
  
  <p>Accidentally found another solution, but I'm not satisfied with it completely:</p>
  
  <pre><code>protocol GenericClassDelegate : class {
      associatedtype ItemType
      func didDoThat(who : GenericClass&lt;ItemType, Self&gt;)
  }
  
  class GenericClass&lt;T, Delegate : GenericClassDelegate&gt; where Delegate.ItemType == T {
      weak var delegate : Delegate?
  
      func notify() {
          delegate?.didDoThat(who: self)
      }
  
      init(_ delegate : Delegate?) {
          self.delegate = delegate
      }
  }
  
  // Delegates must be final classes, otherwise it does not compile
  // because I used Self in GenericClassDelegate
  final class GenericClassDelegateImp&lt;T&gt; : GenericClassDelegate {
      typealias ItemType = T
      func didDoThat(who: GenericClass&lt;T, GenericClassDelegateImp&gt;) {
          print(who)
      }
  }
  
  // Usage:
  var delegate = GenericClassDelegateImp&lt;Int&gt;()
  var genericClass = GenericClass&lt;Int, GenericClassDelegateImp&lt;Int&gt;&gt;(delegate)
  </code></pre>