title: Swift: func return type inheritting class and conforming one or more protocols
body: <p>From an external library I've got the following scenario.</p>
  
  <p>The protocol and base class:</p>
  
  <pre><code>protocol P1 {
      // P1 Stuff
  }
  
  class A {
      // A Stuff
  }
  </code></pre>
  
  <p>Then there is an extension, which causes my troubles. It is defined for the combination of types <code>P1</code> and <code>A</code>.</p>
  
  <pre><code>extension P1 where Self : A {
      public func myDesiredFunc() {
          // Do stuff.
      }
  }
  </code></pre>
  
  <p>And finally there are the implementations <code>B1</code> - <code>Bn</code>, which I use.</p>
  
  <pre><code>class B1 : A, P1 {
  }
  class B2 : A, P1 {
  }
  ...
  </code></pre>
  
  <p>In my code I need to put together instances of B-classes and work with them. The problem is, that I need to use the extension func <code>myDesiredFunc()</code>.  So I need somehow define, that the array type is something like <code>[(A &amp; P1)]</code> and the return type of producing funciton is <code>(A &amp; P1)</code> as well.</p>
  
  <p>However, with this code:</p>
  
  <pre><code>func createBeeInstance() -&gt; (A &amp; P1) {
      if something {
          return B1()
      } else if something {
          return B2()
      }
      ...
  }
  </code></pre>
  
  <p>I'm getting the error:</p>
  
  <blockquote>
    <p>Non-protocol type 'A' cannot be used within a protocol composition.</p>
  </blockquote>
  
  <p>Is there a way to say that the return type is a composition of class and protocol? I'm not able to change the scenario because it's an external library.</p>