title: What's with empty arrays and generics in Swift?
body: <p>Ran across an interesting bug earlier today when working with generics in Swift. I figured out a solution, but I'm wondering if anyone can answer why the compiler does not catch something like this. Let me start with a block of code.</p>
  
  <pre><code>func doSomething&lt;T&gt;(with array: [T]) {
      type(of: array)     // Optional&lt;Array&lt;Int&gt;&gt;
      array is [Int]      // true 
      array is [String]   // true 
  }
  
  var arrayOfInts: [Int] = []
  doSomething(with: arrayOfInts)
  </code></pre>
  
  <p>See line 4. Why the heck is that <code>true</code>? Am I missing something? Shouldn't the compiler be smart enough to figure out this isn't an array of <code>String</code>s? This ultimately led to a bug where a value was set incorrectly due to the empty array assumed to be of the wrong type.</p>
  
  <p>As far as a solution, I went with something along the lines of:</p>
  
  <pre><code>if type(of: array).Element.self == Model.self
  </code></pre>