<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 1, 2016, at 2:46 AM, Xiaodi Wu 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 dir="ltr" style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: 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="">On Mon, Oct 31, 2016 at 8:37 PM, Joe Groff via swift-evolution<span class="Apple-converted-space">&nbsp;</span><span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span><span class="Apple-converted-space">&nbsp;</span>wrote:<br class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;">Sorry for piling onto the bikeshed. We do already have a notation for testing that an Optional isn't nil, `x != nil`. We could theoretically bless `&lt;decl ref&gt; != nil` as a statement condition to also unwrap the referenced declaration in the scope guarded by the condition. (`&lt;decl ref&gt; is T` could similarly rebind a declaration as the cast type.)<br class=""></blockquote><div class=""><br class=""></div><div class="">I think we'd have some weirdness. For instance:</div><div class=""><br class=""></div><div class="">```</div><div class=""><div class="">guard x != nil || x == y else { break }</div><div class="">// oops, now x isn't unwrapped anymore because I added a condition<br class=""></div></div><div class="">```</div></div></div></div></div></blockquote><div><br class=""></div>This is what I suggested a few emails back, but perhaps failed to explain myself that well - my suggestion, however, used the "nonnil" keyword instead, which would prevent you from adding || condition:</div><div><br class=""></div><div>guard nonnil x, x == y else { break }</div><div><br class=""></div><div>To me this reads more naturally than guard unwrap x, x == y else { break }</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: 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=""><div class="gmail_extra"><div class="gmail_quote"><div class=""><br class=""></div><div class="">Also, it'd be unexpected for it to be blessed for guard but not if:</div><div class=""><br class=""></div><div class="">```</div><div class="">if x != nil {</div><div class="">&nbsp; // is x unwrapped here?</div><div class="">&nbsp; // if so, this would be source-breaking...</div><div class="">&nbsp; // if not, it would be surprisingly inconsistent</div><div class="">}</div><div class="">```</div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><br class="">-Joe<br class=""><div class="gmail-HOEnZb"><div class="gmail-h5"><br class="">&gt; On Oct 28, 2016, at 3:34 PM, Erica Sadun via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">&gt;<br class="">&gt;<br class="">&gt;&gt; On Oct 26, 2016, at 11:39 AM, Chris Lattner via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">&gt;&gt;<br class="">&gt;&gt;<br class="">&gt;&gt;&gt; On Oct 26, 2016, at 10:23 AM, Joshua Alvarado &lt;<a href="mailto:alvaradojoshua0@gmail.com" class="">alvaradojoshua0@gmail.com</a>&gt; wrote:<br class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt; In your example the keyword only makes sense if you are shadowing the optional variable. How would unwrap work with a different name?<br class="">&gt;&gt;<br class="">&gt;&gt; It wouldn’t: “unwrap” would never include an equal sign.&nbsp; If you want to do that, use a standard "if let”.<br class="">&gt;&gt;<br class="">&gt;&gt; -Chris<br class="">&gt;<br class="">&gt; So I can stop thinking about this. Gist is here:<span class="Apple-converted-space">&nbsp;</span><a href="https://gist.github.com/erica/db9ce92b3d23cb20799460f603c0ae7c" rel="noreferrer" target="_blank" class="">https://gist.github.com/erica/<wbr class="">db9ce92b3d23cb20799460f603c0ae<wbr class="">7c</a><br class="">&gt;<br class="">&gt; -- E<br class="">&gt;<br class="">&gt;<br class="">&gt; Introducing unwrap<br class="">&gt;<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;• Proposal: TBD<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;• Author: Erica Sadun, Chris Lattner, David Goodine<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;• Status: TBD<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;• Review manager: TBD<br class="">&gt; Introduction<br class="">&gt;<br class="">&gt; This proposal introduces unwrap, simplifying common shadowing and allowing a unified syntax for one-item associated values such as Result types.<br class="">&gt;<br class="">&gt; Swift-evolution thread: guard let x = x<br class="">&gt;<br class="">&gt; Motivation<br class="">&gt;<br class="">&gt; Swift lacks a unified, safe way to bind an optional or single-value enumeration to a shadowed varaiable that is guaranteed to be the same name. Introducing unwrap ensures the conditionally bound item does not accidentally shadow any other item.<br class="">&gt;<br class="">&gt; Compare:<br class="">&gt;<br class="">&gt; guard let foobar = foobar else { …<br class="">&gt;&nbsp; }<br class="">&gt;<br class="">&gt; guard unwrap foobar else { … }<br class="">&gt; Using unwrap eliminates repetition ("foobar = foobar" fails DRY principles) and retains clarity. The keyword is common, simple to understand, and easy to search for if Swift users are unfamiliar with it.<br class="">&gt;<br class="">&gt; This syntax simplifies one-item associated value enumerations by offering a common syntax. Compare:<br class="">&gt;<br class="">&gt; enum Result&lt;T&gt; { case success(T), error(Error<br class="">&gt; ) }<br class="">&gt;<br class="">&gt;<br class="">&gt; guard case let .success(value) = result else { ...<br class="">&gt;&nbsp; }<br class="">&gt;<br class="">&gt; guard unwrap result else { ... }<br class="">&gt; In the latter case result is bound to the wrapped value. Again, it is simpler and clearer, even with non-Optional types.<br class="">&gt;<br class="">&gt; Detailed Design<br class="">&gt;<br class="">&gt; unwrap can be used with any one-value enumeration. The unwrapped value is bound to the same symbol as the associated type.<br class="">&gt;<br class="">&gt; enum TypeName&lt;T, U&gt; { case anycase(T), anothercase(U) }<br class="">&gt;<br class="">&gt; // First and second are type `TypeName`<br class="">&gt; let first = TypeName.anyCase(value1)<br class="">&gt; let second = TypeName. anothercase(value2)<br class="">&gt;<br class="">&gt; guard unwrap first else { ... }<br class="">&gt; // first is now shadowed as type T<br class="">&gt;<br class="">&gt; guard unwrap second else { ... }<br class="">&gt; // second is now shadowed as type U<br class="">&gt;<br class="">&gt; Impact on Existing Code<br class="">&gt;<br class="">&gt; This change is additive and has no impact on existing code other than intentional refactoring.<br class="">&gt;<br class="">&gt; Timeline<br class="">&gt;<br class="">&gt; This proposal is additive and not suited for consideration until Swift 4 phase 2<br class="">&gt;<br class="">&gt; Alternatives Considered<br class="">&gt;<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;• Using a bind keyword. Past discussions were held in the first week of February 2016.<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;• Fixing pattern matching grammar<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;• Not using this approach<br class="">&gt;<br class=""></div></div><div class="gmail-HOEnZb"><div class="gmail-h5">&gt; ______________________________<wbr class="">_________________<br class="">&gt; swift-evolution mailing list<br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a><br class=""><br class="">______________________________<wbr class="">_________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a><br class=""></div></div></blockquote></div><br class=""></div></div><span style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: 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="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: 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=""><span style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: 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="">swift-evolution mailing list</span><br style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: 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=""><a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: 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-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: 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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: 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-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></body></html>