<div dir="ltr">Currently, the language syntax only allows type identifiers in the type-casting-operator productions:<div><br></div><div>&quot; &quot; surrounds a keyword</div><div><br></div><div>type-casting-operator -&gt; &quot;is&quot; type</div><div>type-casting-operator -&gt; &quot;as&quot; type</div><div><div>type-casting-operator -&gt; &quot;as&quot; &quot;?&quot; type</div></div><div><div>type-casting-operator -&gt; &quot;as&quot; &quot;!&quot; type</div></div><div><br></div><div>The type production doesn&#39;t allow for expressions which resolve to a type, only explicit type references.</div><div><br></div><div>So, if you want to refer to the item *as* its dynamic type, there&#39;s no direct way to do that unless you declare the name of the type in code:</div><div><br></div><div>class Example {}</div><div>var value: Any = Example()</div><div>var again = value as value.dynamicType</div><div>// doesn&#39;t work because value.dynamicType is an expression</div><div><br></div><div>It should be possible to upcast to dynamicType immediately with no chance of failure. To that end, I suggest adding two productions:</div><div><br></div><div>type-casting-operator -&gt; &quot;is&quot; &quot;dynamicType&quot;</div><div>type-casting-operator -&gt; &quot;as&quot; &quot;dynamicType&quot;</div><div><br></div><div>Following along the example above, we could then do:</div><div><br></div><div>var nowPossible = value as dynamicType</div><div><br></div><div>and have nowPossible be Example.Type.</div><div><br></div><div>Possible use cases for this functionality include:</div><div><br></div><div>1. Dealing with mixed-type collections from Objective-C code:</div><div>   @[ @&quot;key1&quot;, @5, @&quot;key2&quot;, @20, @&quot;key3&quot;, @[@&quot;red&quot;,@&quot;green&quot;,@&quot;blue&quot;]]</div><div><br></div><div>2. Taking advantage of type-specific polymorphism without having to modify code:</div><div><br></div><div>    func handleObject(obj: NSNumber) { print(&quot;Number&quot;) }</div><div>    func handleObject(obj: NSData) { print(&quot;Data&quot;) }</div><div><br></div><div>    func dispatchToHandler(kind: AnyObject) {</div><div>        print(&quot;dispatching \(kind.dynamicType)&quot;)</div><div>        handleObject(kind as dynamicType)</div><div>    }</div><div><br></div><div>I suspect that handling this use case might pose the most difficulties when implementing the feature. It might make the whole thing impossible if there&#39;s no way to resolve types at run-time in compiled code. For example, if the dispatcher is in a Framework distributed as a binary and the user does</div><div><br></div><div>class MyClass {}</div><div>func handleObject(obj: MyClass) { print &quot;Success!&quot; }</div><div><br></div><div>let stuff = MyClass()</div><div>dispatchToHandler(stuff)</div><div><br></div><div>in the project&#39;s code, what would happen?</div><div><br></div><div>Casting to an intermediate type between the static and dynamic types would fall out naturally, though in that case you&#39;d already have to know the dynamicType and write the explicit intermediate type name in the code. If that much is known then it&#39;s possible to cast directly to that intermediate type with existing syntax.</div><div><br></div><div>Also, it&#39;s worth noting that the &quot;Any&quot; case is only the broadest instance possible. Anything that passes data along as a super type, by using a Protocol as a concrete type specifier, etc. could benefit from this mechanism.</div><div><br></div><div>Mike</div></div>