title: Swift: check if generic type conforms to protocol
body: <p>I have a protocol that I defined like so:</p>
  
  <pre><code>protocol MyProtocol {
     ...
  }
  </code></pre>
  
  <p>I also have a generic struct:</p>
  
  <pre><code>struct MyStruct &lt;T&gt;  {
      ...
  }
  </code></pre>
  
  <p>Finally I have a generic function:</p>
  
  <pre><code>func myFunc &lt;T&gt; (s: MyStruct&lt;T&gt;) -&gt; T? {
     ...
  }
  </code></pre>
  
  <p>I'd like to test inside of the function if the type T conforms to MyProtocol. Essentially I'd like to be able to do (~ pseudocode):</p>
  
  <pre><code>let conforms = T.self is MyProtocol
  </code></pre>
  
  <p>But this throws a compiler error:</p>
  
  <pre><code>error: cannot downcast from 'T.Type' to non-@objc protocol type 'MyProtocol'
     let conforms = T.self is MyProtocol
                    ~~~~~~ ^  ~~~~~~~~~~
  </code></pre>
  
  <p>I have also tried variations, like <code>T.self is MyProtocol.self</code>, <code>T is MyProtocol</code>, and using <code>==</code> instead of <code>is</code>. So far I haven't gotten anywhere. Any ideas?</p>