<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><blockquote type="cite" class=""><div class="">On Jan 8, 2016, at 6:14 PM, Charles Srstka via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">On Jan 8, 2016, at 7:56 PM, Michael Henson via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></blockquote><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I've spent some time exploring different aspects of the language's type system and came across a case where I wanted to know the type of the thing an optional might hold, if it exists. The direct type of an optional variable is, unsurprisingly, an optional:<div class=""><br class=""></div><div class="">&gt; var example: String? = "example"</div><div class="">example: String? = "example"</div><div class="">&gt; example.dynamicType</div><div class="">$R1: String?.Type = String?</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Ok, that didn't work. What if I dereference?</div><div class=""><br class=""></div><div class="">&gt; example!.dynamicType</div><div class="">$R2: String.Type = String</div><div class=""><br class=""></div><div class="">Ok. What if the optional doesn't currently have a value? Does this still work?</div><div class=""><br class=""></div><div class="">&gt; example = nil</div><div class="">$R3: String.Type = String</div><div class=""><br class=""></div><div class="">That's odd. I expected it not work, given how forced unwrapping of nil optionals usually turns out.</div><div class=""><br class=""></div><div class="">Is this explicitly intended behavior, something that really shouldn't work and shouldn't be relied on, or... other?</div></div></div></blockquote><br class=""></div><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Well, there’s at least one case where it doesn’t work, since this only seems to work with ‘var’ variables, and not with ‘let’ constants.</span><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="" style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;"><span class="" style="color: rgb(169, 169, 169);">&nbsp;1&gt;<span class="Apple-converted-space">&nbsp;</span></span>var variable: String? = nil</div><div class="" style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;">variable: String? = nil</div><div class="" style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;"><span class="" style="color: rgb(169, 169, 169);">&nbsp; 2&gt;<span class="Apple-converted-space">&nbsp;</span></span>let constant: String? = nil</div><div class="" style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;">constant: String? = nil</div><div class="" style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;"><span class="" style="color: rgb(169, 169, 169);">&nbsp; 3&gt;<span class="Apple-converted-space">&nbsp;</span></span>variable!.dynamicType</div><div class="" style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;">$R0: String.Type = String</div><div class="" style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;"><span class="" style="color: rgb(169, 169, 169);">&nbsp; 4&gt;<span class="Apple-converted-space">&nbsp;</span></span>constant!.dynamicType</div><div class="" style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;">fatal error: unexpectedly found nil while unwrapping an Optional value</div><div class="" style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;">Execution interrupted. Enter Swift code to recover and continue.</div><div class="" style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;">Enter LLDB commands to investigate (type :help for assistance.)</div></div><div class="" style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;"><br class=""></div><div class="" style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;"><span class="" style="font-family: Helvetica; font-size: 12px;">I, too, am confused as to why this ever works at all.</span></div></div></div></blockquote><br class=""></div><div>Please file a bug; we might be failing to evaluate the base when we can statically fold the .dynamicType expression.</div><div><br class=""></div><div>John.</div><br class=""></body></html>