title: From the Swift REPL, how do I get the static type of an expression?
body: <p>From the Swift REPL, how do I get the static type of an expression? </p>
  
  <p>For instance, in Haskell, I can use <code>:type</code> to get the type of an expression:</p>
  
  <pre><code>Prelude&gt; :type "hello"
  "hello" :: [Char]
  
  Prelude&gt; :type (1 + 1)
  (1 + 1) :: Num a =&gt; a
  </code></pre>
  
  <p>This is indicating that the type of "hello" is a list of <code>Char</code>, and the type of (1 + 1) is some type <code>a</code> in the type class <code>Num</code>.</p>
  
  <p>There is no obvious way to do this on the Swift REPL but I think it might be possible with the facilities the language already provides. Here is what I've figured out so far. I would be delighted if anyone else holds the missing piece of the puzzle.</p>
  
  <p>These are the various alternatives, none of which is quite right:</p>
  
  <h2>1. dynamicType</h2>
  
  <p>As the name implies, dynamicType returns the dynamically determined type of the instance, not the static type of the expression which evaluates to the instance. Also, it does not seem to work from the REPL for Swift types:</p>
  
  <pre><code>1&gt; 1.dynamicType
  error: could not fetch result -- Couldn't apply expression side effects : Couldn't dematerialize result: corresponding symbol wasn't found
  2&gt; struct Foo { let val:Int = 1 }
  3&gt; let f = Foo()
  4&gt; f.dynamicType
  error: could not fetch result -- Couldn't apply expression side effects : Couldn't dematerialize result: corresponding symbol wasn't found
  </code></pre>
  
  <h2>2. _stdlib_getDemangledTypeName(foo)</h2>
  
  <p>This function (suggested by a contributor who may wish to remain nameless?) returns a string with the name of the type of foo. However, it evaluates the expression foo. Also, it does not always return the static type, as one can see by running the script below, where the de-mangling function will give the name of one of the randomly computed dynamic types A or B instead of the statically determinable type Any:</p>
  
  <pre><code>#!/usr/bin/swift
  import Foundation
  
  class Letter { } ; class A : Letter {} ; class B : Letter {}
  func randomAny() -&gt; Any { println("side-effecting!"); return (arc4random() % 2) == 0 ? A() : B() }
  let x : Any = randomAny()  // x has the static type of Any
  let typeNameOfX = _stdlib_getDemangledTypeName(x)
  println("the constant x with static type Any reports a type name of : \(typeNameOfX)")
  </code></pre>
  
  <h2>3. a custom generic function</h2>
  
  <p>What sounds like the closest thing was a suggestion of @jckarter on Twitter to to define the following function:</p>
  
  <pre><code>func staticType&lt;T&gt;(@autoclosure () -&gt; T) -&gt; T.Type { return T.self }
  </code></pre>
  
  <p>Since the function captures its argument in a closure, the argument is not evaluated since the closure is never called. However, this has two drawbacks:</p>
  
  <ol>
  <li>Calling it at the REPL only produces an error.</li>
  <li>Calling it in a script returns a MetaType instance, and there seems to be no general way to go from a MetaType instance to human-readable string indicating what type the instance describes.</li>
  </ol>
  
  <p>If we know how to get the name of a MetaType, for ObjC and Swift types, that would be enough to make this last approach work, at least in scripts if not the REPL.</p>