title: Using a Type Variable in a Generic
body: <p>I have <a href="https://stackoverflow.com/questions/7760848/how-to-use-a-type-variable-in-a-generic-method-call-c">this question</a> except for Swift. How do I use a <code>Type</code> variable in a generic?</p>
  
  <p>I tried this:</p>
  
  <pre><code>func intType() -&gt; Int.Type {
      return Int.self
  }
  
  func test() {
      var t = self.intType()
      var arr = Array&lt;t&gt;() // Error: "'t' is not a type". Uh... yeah, it is.
  }
  </code></pre>
  
  <p>This didn't work either:</p>
  
  <pre><code>var arr = Array&lt;t.Type&gt;() // Error: "'t' is not a type"
  var arr = Array&lt;t.self&gt;() // Swift doesn't seem to even understand this syntax at all.
  </code></pre>
  
  <p>Is there a way to do this? I get the feeling that Swift just doesn't support it and is giving me somewhat ambiguous error messages.</p>
  
  <p><strong>Edit</strong>: Here's a more complex example where the problem can't be circumvented using a generic function header. Of course it doesn't make sense, but I have a sensible use for this kind of functionality somewhere in my code and would rather post a clean example instead of my actual code:</p>
  
  <pre><code>func someTypes() -&gt; [Any.Type] {
      var ret = [Any.Type]()
      for (var i = 0; i&lt;rand()%10; i++) {
          if (rand()%2 == 0){ ret.append(Int.self) }
          else {ret.append(String.self) }
      }
      return ret
  }
  
  func test() {
      var ts = self.someTypes()
  
      for t in ts {
          var arr = Array&lt;t&gt;()
      }
  }
  </code></pre>