title: Instantiate an object of a generic type in Swift
body: <p>I am implementing a class <code>Foo</code> in Swift, that is supposed to instantiate objects of a given <strong>subclass</strong> of <code>SuperBar</code>, e.g. <code>Bar: SuperBar</code>. I really like generics in Swift, so I tried implementing it this way:</p>
  
  <pre><code>class Foo&lt;T: SuperBar&gt; {
  
      func instantiateObject() -&gt; T {
          return T()
      }
  
  }
  
  class SuperBar {
  
  }
  
  class Bar: SuperBar {
  
  }
  
  let foo = Foo&lt;Bar&gt;()
  
  let obj = foo.instantiateObject()
  </code></pre>
  
  <p>You can run the code snippet in an Xcode Playground and observe that <code>obj</code> is of type <code>SuperBar</code> instead of <code>Bar</code>, although it says <code>Bar</code> when I <kbd>Alt</kbd>-click on the constant name.</p>
  
  <p><strong>Any Ideas?</strong> :)</p>