<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"><style type="text/css">body { background: rgba(255, 255, 255, 255); }</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 29 Feb 2016, at 10:40, Brent Royal-Gordon via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><blockquote type="cite" class="">Because of the above, I'd like to start a discussion about its deprecation and eventual removal in favor of either an unsafeUnwrap function or other extra-syntactic constructs.<br class=""></blockquote><br class="">I could not disagree more strongly.<br class=""><br class="">Swift takes the position that assertions are good and that it's better to crash in an unexpected state than to limp on. Force unwrapping is an example of that philosophy. A force unwrap is a "this is not nil" assertion.<br class=""></div></div></blockquote></div><div class=""><br class=""></div><div class="">+(0.5 + 0.5). I think both of you have a point.</div><div class=""><br class=""></div><div class="">To me, even after soon two years of Swift, it's still extremely hard to visually see where forced unwrapping happens. I do&nbsp;<b class="">not</b>&nbsp;think we should recommend&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">unsafeUnwrap</span>&nbsp;though because we'd then trade a guaranteed crash&nbsp;for undefined behaviour&nbsp;on&nbsp;<span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">nil</span>, which is definitely the wrong choice at least 95% of time.</div><div class=""><br class=""></div><div class="">Unless we're getting an option in Xcode to make the exclamation point U+0021 render as something like ❗️ (U+2757 U+FE0F), I think it would be more sensible to rather have a standard member function on Optional that does the same but more explicitly:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class="">extension<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Optional</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp;&nbsp;<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> unwrap() -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Wrapped</span> {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">guard</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> value = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span> {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">fatalError</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span>"unexpectedly found nil where unwrapping an <span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">\</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Optional</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>) value"<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;}</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> value</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp;&nbsp;}</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> x: </span>String<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">?</span></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal; color: rgb(49, 89, 93);" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">x</span> != <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">x</span>.unwrap()) }</div></div></div></blockquote><div class=""><br class=""></div><div class="">Indeed, with <span style="font-family: Menlo; font-size: 11px;" class="">Optional.unwrap</span>&nbsp;as a member function, we could even report <i class="">where</i> the assertion failed, which could be a big help when debugging errors that slipped into production:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class="">extension<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Optional</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp;&nbsp;<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> unwrap(file file: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">StaticString</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">#file</span>, line: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">#line</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Wrapped</span> { ... }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">x</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>unwrap<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">())</span></div></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" color="#ff2600" class=""><span style="font-size: 11px;" class="">fatal error: unexpectedly found nil where unwrapping an Optional&lt;String&gt; value: file /path/to/endless/swift-evolution/this-email-message.swift, line 36</span></font></div></div></blockquote><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">There could be compiler magic to replace calls to&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">Optional.unwrap</span>&nbsp;with&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">unsafeUnwrap</span>&nbsp;in&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">-Ounchecked</span>&nbsp;builds.</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Naming alternatives considered:&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">forceUnwrap</span>,&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">forcedUnwrap</span>,&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">unwrapValue</span>,&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">assertUnwrap</span>.</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">— Pyry</div></div></body></html>