<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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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"> </span><span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span><span class="Apple-converted-space"> </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 `<decl ref> != nil` as a statement condition to also unwrap the referenced declaration in the scope guarded by the condition. (`<decl ref> 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=""> // is x unwrapped here?</div><div class=""> // if so, this would be source-breaking...</div><div class=""> // 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="">> On Oct 28, 2016, at 3:34 PM, Erica Sadun via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class="">><br class="">><br class="">>> On Oct 26, 2016, at 11:39 AM, Chris Lattner via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class="">>><br class="">>><br class="">>>> On Oct 26, 2016, at 10:23 AM, Joshua Alvarado <<a href="mailto:alvaradojoshua0@gmail.com" class="">alvaradojoshua0@gmail.com</a>> wrote:<br class="">>>><br class="">>>> 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="">>><br class="">>> It wouldn’t: “unwrap” would never include an equal sign. If you want to do that, use a standard "if let”.<br class="">>><br class="">>> -Chris<br class="">><br class="">> So I can stop thinking about this. Gist is here:<span class="Apple-converted-space"> </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="">><br class="">> -- E<br class="">><br class="">><br class="">> Introducing unwrap<br class="">><br class="">> • Proposal: TBD<br class="">> • Author: Erica Sadun, Chris Lattner, David Goodine<br class="">> • Status: TBD<br class="">> • Review manager: TBD<br class="">> Introduction<br class="">><br class="">> This proposal introduces unwrap, simplifying common shadowing and allowing a unified syntax for one-item associated values such as Result types.<br class="">><br class="">> Swift-evolution thread: guard let x = x<br class="">><br class="">> Motivation<br class="">><br class="">> 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="">><br class="">> Compare:<br class="">><br class="">> guard let foobar = foobar else { …<br class="">> }<br class="">><br class="">> guard unwrap foobar else { … }<br class="">> 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="">><br class="">> This syntax simplifies one-item associated value enumerations by offering a common syntax. Compare:<br class="">><br class="">> enum Result<T> { case success(T), error(Error<br class="">> ) }<br class="">><br class="">><br class="">> guard case let .success(value) = result else { ...<br class="">> }<br class="">><br class="">> guard unwrap result else { ... }<br class="">> In the latter case result is bound to the wrapped value. Again, it is simpler and clearer, even with non-Optional types.<br class="">><br class="">> Detailed Design<br class="">><br class="">> unwrap can be used with any one-value enumeration. The unwrapped value is bound to the same symbol as the associated type.<br class="">><br class="">> enum TypeName<T, U> { case anycase(T), anothercase(U) }<br class="">><br class="">> // First and second are type `TypeName`<br class="">> let first = TypeName.anyCase(value1)<br class="">> let second = TypeName. anothercase(value2)<br class="">><br class="">> guard unwrap first else { ... }<br class="">> // first is now shadowed as type T<br class="">><br class="">> guard unwrap second else { ... }<br class="">> // second is now shadowed as type U<br class="">><br class="">> Impact on Existing Code<br class="">><br class="">> This change is additive and has no impact on existing code other than intentional refactoring.<br class="">><br class="">> Timeline<br class="">><br class="">> This proposal is additive and not suited for consideration until Swift 4 phase 2<br class="">><br class="">> Alternatives Considered<br class="">><br class="">> • Using a bind keyword. Past discussions were held in the first week of February 2016.<br class="">> • Fixing pattern matching grammar<br class="">> • Not using this approach<br class="">><br class=""></div></div><div class="gmail-HOEnZb"><div class="gmail-h5">> ______________________________<wbr class="">_________________<br class="">> swift-evolution mailing list<br class="">><span class="Apple-converted-space"> </span><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">><span class="Apple-converted-space"> </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>