title: How to call a generic function with a dynamic type in Swift
body: <p>Say I have a protocol <code>Fooable</code>:</p>
  
  <pre><code>protocol Fooable {}
  </code></pre>
  
  <p>Now I need to work with a <code>Fooable</code> type in a generic function:</p>
  
  <pre><code>func fooingAround&lt;FooableType: Fooable&gt;(withType: FooableType.Type) {}
  </code></pre>
  
  <p>This works fine when I just call the function with a <code>Fooable</code> type:</p>
  
  <pre><code>struct Foo: Fooable {}
  fooingAround(Foo.self) // works fine
  </code></pre>
  
  <p>However, I need to retrieve the <code>Fooable</code> type that I hand over to the function from somewhere else. This is where the compiler fails:</p>
  
  <pre><code>let fooableType: Fooable.Type = // obtain from somewhere else
  fooingAround(fooableType) // compiler error: "Cannot invoke 'fooingAround' with an argument list of type '(Fooable.Type)'"
  </code></pre>
  
  <p>Specifically, I obtain the <code>Fooable.Type</code> from an enum that describes API endpoints, where each endpoint is represented by a different <code>Fooable</code> class.</p>
  
  <p>I suppose the problem arises because I <em>dynamically</em> obtain a type, so there can be no strong typing at compile time.</p>
  
  <p>Is there a way to work around this?</p>