<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class=""><div class=""></div><blockquote type="cite" class=""><div class="">func magic(_ variable: Any) {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let foo = variable as dynamicType</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// ???</div><div class="">}</div><div class=""><br class=""></div><div class="">magic("hello")</div><div class="">magic(42)</div></blockquote><div class=""><br class=""></div>What would be the result of this? What benefit do I get from casting it to its dynamic type?<br class=""><div class="">
<br class="Apple-interchange-newline"><span style="color: rgb(0, 0, 0); font-family: 'Lucida Grande'; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; display: inline !important; float: none;" class="">Félix</span>
</div>

<br class=""><div><blockquote type="cite" class=""><div class="">Le 8 janv. 2016 à 23:40:46, Michael Henson via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Currently, the language syntax only allows type identifiers in the type-casting-operator productions:<div class=""><br class=""></div><div class="">" " surrounds a keyword</div><div class=""><br class=""></div><div class="">type-casting-operator -&gt; "is" type</div><div class="">type-casting-operator -&gt; "as" type</div><div class=""><div class="">type-casting-operator -&gt; "as" "?" type</div></div><div class=""><div class="">type-casting-operator -&gt; "as" "!" type</div></div><div class=""><br class=""></div><div class="">The type production doesn't allow for expressions which resolve to a type, only explicit type references.</div><div class=""><br class=""></div><div class="">So, if you want to refer to the item *as* its dynamic type, there's no direct way to do that unless you declare the name of the type in code:</div><div class=""><br class=""></div><div class="">class Example {}</div><div class="">var value: Any = Example()</div><div class="">var again = value as value.dynamicType</div><div class="">// doesn't work because value.dynamicType is an expression</div><div class=""><br class=""></div><div class="">It should be possible to upcast to dynamicType immediately with no chance of failure. To that end, I suggest adding two productions:</div><div class=""><br class=""></div><div class="">type-casting-operator -&gt; "is" "dynamicType"</div><div class="">type-casting-operator -&gt; "as" "dynamicType"</div><div class=""><br class=""></div><div class="">Following along the example above, we could then do:</div><div class=""><br class=""></div><div class="">var nowPossible = value as dynamicType</div><div class=""><br class=""></div><div class="">and have nowPossible be Example.Type.</div><div class=""><br class=""></div><div class="">Possible use cases for this functionality include:</div><div class=""><br class=""></div><div class="">1. Dealing with mixed-type collections from Objective-C code:</div><div class="">&nbsp; &nbsp;@[ @"key1", @5, @"key2", @20, @"key3", @[@"red",@"green",@"blue"]]</div><div class=""><br class=""></div><div class="">2. Taking advantage of type-specific polymorphism without having to modify code:</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; func handleObject(obj: NSNumber) { print("Number") }</div><div class="">&nbsp; &nbsp; func handleObject(obj: NSData) { print("Data") }</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; func dispatchToHandler(kind: AnyObject) {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; print("dispatching \(kind.dynamicType)")</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; handleObject(kind as dynamicType)</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">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'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 class=""><br class=""></div><div class="">class MyClass {}</div><div class="">func handleObject(obj: MyClass) { print "Success!" }</div><div class=""><br class=""></div><div class="">let stuff = MyClass()</div><div class="">dispatchToHandler(stuff)</div><div class=""><br class=""></div><div class="">in the project's code, what would happen?</div><div class=""><br class=""></div><div class="">Casting to an intermediate type between the static and dynamic types would fall out naturally, though in that case you'd already have to know the dynamicType and write the explicit intermediate type name in the code. If that much is known then it's possible to cast directly to that intermediate type with existing syntax.</div><div class=""><br class=""></div><div class="">Also, it's worth noting that the "Any" 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 class=""><br class=""></div><div class="">Mike</div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=iRI3beHTe3UxYAHTlV3lA38zIPfHMhyuRzgTmGKV6k7rruDOPX9BBzrCoAcmnjZRz7rE3ucQORXeg-2FO1vE9OFI5CwoOtqxeNJaQyJuCf1x3MUXP-2BFlsAadjWSj1ANL70K7vbZXGb9Ber6TBiVwXhhHm9YZef68Nx3sNsjJRW0pGvJrRoEBaFFkxNspzbg5rIbhNg9T4ZNy4CdXaAb9HIEPfNRLjKjPF9PCjVpouMo-2B0-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>