<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=""><blockquote type="cite" class="">On Jan 8, 2016, at 7:56 PM, Michael Henson via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></blockquote><div><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="">> var example: String? = "example"</div><div class="">example: String? = "example"</div><div class="">> 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="">> 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="">> 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>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.<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 1> </span>var variable: String? = nil</div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class="">variable: String? = nil</div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 2> </span>let constant: String? = nil</div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class="">constant: String? = nil</div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 3> </span>variable!.dynamicType</div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class="">$R0: String.Type = String</div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 4> </span>constant!.dynamicType</div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class="">fatal error: unexpectedly found nil while unwrapping an Optional value</div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class="">Execution interrupted. Enter Swift code to recover and continue.</div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class="">Enter LLDB commands to investigate (type :help for assistance.)</div></div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class=""><br class=""></div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">I, too, am confused as to why this ever works at all.</span></div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">Charles</span></div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div></body></html>