title: Inheritance, Generics, and Protocols in Swift
body: <p>I have a generic class of the form:</p>
  
  <pre><code>class BaseClass&lt;T&gt; {
      var prop: T
      ...
  }
  </code></pre>
  
  <p>I then have multiple subclasses of the form:</p>
  
  <pre><code>class SubClassOne: BaseClass&lt;SomeSubClass&gt; {
      ...
  }
  
  class SubClassTwo: BaseClass&lt;SomeOtherSubClass&gt; {
      ...
  }
  </code></pre>
  
  <p>Where the type parameters <code>SomeSubClass</code> and <code>SomeOtherSubClass</code> both inherit from a common base class <code>SomeBaseClass</code>.</p>
  
  <p>I now want to define a variable to store instances of both <code>SubClassOne</code> and <code>SubClassTwo</code>. I have tried many possibilities:</p>
  
  <pre><code>var obj: BaseClass
  var obj: BaseClass&lt;SomeBaseClass&gt;
  var obj: BaseClass&lt;Any&gt;
  </code></pre>
  
  <p>But the first attempt results in the error <code>Reference to generic type 'BaseClass' requires arguments in &lt;...&gt;</code>, and the other two result in the error <code>Cannot assign value of type 'SubClassOne' to type ...</code> when trying to assign a value. I even tried to trick the Swift compiler into inferring the type for me by initializing an array:</p>
  
  <pre><code>var testArray = [SubClassOne(), SubClassTwo()]
  </code></pre>
  
  <p>But even this failed, resulting in the error <code>Heterogeneous collection literal could only be inferred to [Any]; add explicit type annotation if this is intentional</code>. Indeed, the only type annotation that successfully allows storage of both SubClasses is <code>Any</code> or <code>AnyObject</code>. Is it possible to store these instances with a more specific type? If not, why?</p>
  
  <p>The reason it's important to do so is that I ultimately want to get the property <code>prop</code> from the stored variable <code>obj</code>. I am unable to do so if <code>obj</code> is stored as <code>Any</code>. I am also unable to simply cast it to <code>SubClassOne</code> or <code>SubClassTwo</code> because the method itself where I am trying to access the properties is a generic method, and which of <code>SubClassOne</code> or <code>SubClassTwo</code> to cast to depends on the generic type parameter of the method:</p>
  
  <pre><code>func castObj&lt;T&gt;(asType: T.Type) {
      (self.obj as? T).prop
  }
  </code></pre>
  
  <p>Which would be called as: <code>castObj(asType: SubClassOne.self)</code> or <code>castObj(asType: SubClassTwo.self)</code>. However, we run into the same problem: the only generic type parameter constraint I can define that accepts both <code>SubClassOne</code> and <code>SubClassTwo</code> is <code>Any</code>, and then the Swift compiler complains: <code>Value of type 'T' has no member 'prop'</code>.</p>
  
  <p>As a workaround I tried to define a protocol that encapsulates the desired property:</p>
  
  <pre><code>protocol HasProp {
      var prop: SomeBaseClass { get }
  }
  </code></pre>
  
  <p>Then I added this to the declaration of <code>SubClassOne</code> and <code>SubClassTwo</code>. However this resulted in still another error: <code>Type 'SubClassOne' does not conform to protocol 'HasProp'</code>. This confuses me as well, since <code>SubClassOne</code> and <code>SubClassTwo</code> both inherit <code>prop</code> from <code>BaseClass&lt;SomeSubClass&gt;</code> and so actually do conform to the protocol.</p>
  
  <p>In summary:</p>
  
  <ol>
  <li>Is it possible to store instances of <code>SubClassOne</code> and <code>SubClassTwo</code> with a more specific type that gives access to properties of <code>BaseClass</code>? If not, why?</li>
  <li>Why do the SubClasses not conform to the protocol as expected?</li>
  <li>How can I change the design to attain my desired behavior?</li>
  </ol>