title: Apple Swift: Type Casting Generics
body: <p>I'm writing some Swift code where I have an array containing a generic type:</p>
  
  <pre><code>let _data: Array&lt;T&gt; = T[]()
  </code></pre>
  
  <p>Later in my code I need to determine the type stored in the array. I tried using the type casting technique described in the <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html#//apple_ref/doc/uid/TP40014097-CH22-XID_443">documentation</a> (although it was not used for generics).</p>
  
  <pre><code>switch self._data {
  case let doubleData as Array&lt;Double&gt;:
    // Do something with doubleData
  case let floatData as Array&lt;Float&gt;:
    // Do something with floatData
  default:
    return nil // If the data type is unknown return nil
  }
  </code></pre>
  
  <p>The above switch statement results in the following error upon compilation:</p>
  
  <blockquote>
    <ol>
    <li>While emitting IR SIL function @_TFC19Adder_Example___Mac6Matrix9transposeUS_7Element__fGS0_Q__FT_GSqGS0_Q___
    for 'transpose' at /code.viperscience/Adder/src/Adder
    Library/Matrix.swift:45:3 :0: error: unable to execute
    command: Segmentation fault: 11 :0: error: swift frontend
    command failed due to signal (use -v to see invocation) Command
    /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
    failed with exit code 254</li>
    </ol>
  </blockquote>
  
  <p>Anybody know how I can cast my generic data to its actual type in order to take specific action?</p>