<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="">It doesn't in a sense "it currently works like this", but it would be theoretically possible to have the compiler understand some static conditions, such as<div class=""><br class=""></div><div class="">/// x is Int</div><div class="">guard x > 0 else {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>return </div><div class="">}</div><div class=""><br class=""></div><div class="">if x == -1 { // Warning</div><div class="">...</div><div class=""><br class=""></div><div class="">Then when you'd compare x == -1, you could get a warning that this condition is always false since you've previously stated that x is greater than zero.</div><div class=""><br class=""></div><div class="">In this sense, you could view this as </div><div class=""><br class=""></div><div class="">/// x is Any?</div><div class="">guard x != nil else ...</div><div class=""><br class=""></div><div class="">which generally is a condition that guarantees that x is nonnil further down the scope. Of course, x mustn't be an instance variable that you're referencing without explicit self.</div><div class=""><div class=""><br class=""></div><div class="">And it would break guard foo at all - guard nonnil x else ... would just guarantee that x is nonnil further down the scope, allowing automatic force-unwrapping of the variable - which is generally what is the desired effect.</div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Oct 31, 2016, at 8:16 PM, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">Well, "guard x != nil" does not unwrap x. An additive proposal must make clear the difference between testing if an optional is nil and unwrapping it.<br class=""><br class="">Meanwhile, "guard foo" tests if foo evaluates to true. An additive proposal cannot break this syntax.<br class=""><div class="gmail_quote"><div dir="ltr" class="">On Mon, Oct 31, 2016 at 14:11 Charlie Monroe <<a href="mailto:charlie@charliemonroe.net" class="">charlie@charliemonroe.net</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg">To me, it makes more sense to use</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">guard nonnil x else ...</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">since guard is guarding a condition is true - or am I wrong? Generally, it would tell the compiler that</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">guard x != nil else ...</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">which to me reads like guard nonnil x...</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Just a side-note... </div></div><div style="word-wrap:break-word" class="gmail_msg"><br class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">On Oct 31, 2016, at 8:05 PM, Xiaodi Wu via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br class="gmail_msg m_-4179006634048633020Apple-interchange-newline"><div class="gmail_msg">The proposal is to create a new shorthand "guard unwrap x" for "guard let x = x". The "guard" statement serves purposes other than unwrapping variables and cannot be removed.<br class="gmail_msg"><div class="gmail_quote gmail_msg"><div dir="ltr" class="gmail_msg">On Mon, Oct 31, 2016 at 14:03 Joshua Alvarado <<a href="mailto:alvaradojoshua0@gmail.com" class="gmail_msg" target="_blank">alvaradojoshua0@gmail.com</a>> wrote:<br class="gmail_msg"></div><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto" class="gmail_msg"><div class="gmail_msg">Without going back through the history, is the proposal to keep replace guard or still keep guard keyword and create a new unwrap? If unwrap is added the guard keyword should just be removed. </div></div><div dir="auto" class="gmail_msg"><div id="m_-4179006634048633020m_-1340813727907140397AppleMailSignature" class="gmail_msg"><br class="gmail_msg">Alvarado, Joshua</div></div><div dir="auto" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg">On Oct 31, 2016, at 12:49 PM, Xiaodi Wu via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:<br class="gmail_msg"><br class="gmail_msg"></div><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">Because "guard" already means something...<br class="gmail_msg"><br class="gmail_msg"><br class="gmail_msg"><div class="gmail_quote gmail_msg"><div dir="ltr" class="gmail_msg">On Mon, Oct 31, 2016 at 13:45 Kenny Leung via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:<br class="gmail_msg"></div><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">It seems to me that you would end up typing “guard unwrap” 99% of the time, so why not just let “guard” imply “guard unwrap” 100% of the time?<br class="gmail_msg">
<br class="gmail_msg">
-Kenny<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
> On Oct 31, 2016, at 11:34 AM, Erica Sadun <<a href="mailto:erica@ericasadun.com" class="gmail_msg" target="_blank">erica@ericasadun.com</a>> wrote:<br class="gmail_msg">
><br class="gmail_msg">
> Because there's an action taking place in addition to guarding<br class="gmail_msg">
><br class="gmail_msg">
> -- E<br class="gmail_msg">
><br class="gmail_msg">
>> On Oct 31, 2016, at 12:30 PM, Kenny Leung via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:<br class="gmail_msg">
>><br class="gmail_msg">
>> Why have the “unwrap” keyword at all? Isn’t “guard” the keyword?<br class="gmail_msg">
>><br class="gmail_msg">
>> guard blah else {<br class="gmail_msg">
>> }<br class="gmail_msg">
>><br class="gmail_msg">
>> -Kenny<br class="gmail_msg">
>><br class="gmail_msg">
>><br class="gmail_msg">
>>> On Oct 28, 2016, at 3:34 PM, Erica Sadun via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:<br class="gmail_msg">
>>><br class="gmail_msg">
>>><br class="gmail_msg">
>>>> On Oct 26, 2016, at 11:39 AM, Chris Lattner via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:<br class="gmail_msg">
>>>><br class="gmail_msg">
>>>><br class="gmail_msg">
>>>>> On Oct 26, 2016, at 10:23 AM, Joshua Alvarado <<a href="mailto:alvaradojoshua0@gmail.com" class="gmail_msg" target="_blank">alvaradojoshua0@gmail.com</a>> wrote:<br class="gmail_msg">
>>>>><br class="gmail_msg">
>>>>> 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="gmail_msg">
>>>><br class="gmail_msg">
>>>> It wouldn’t: “unwrap” would never include an equal sign. If you want to do that, use a standard "if let”.<br class="gmail_msg">
>>>><br class="gmail_msg">
>>>> -Chris<br class="gmail_msg">
>>><br class="gmail_msg">
>>> So I can stop thinking about this. Gist is here: <a href="https://gist.github.com/erica/db9ce92b3d23cb20799460f603c0ae7c" rel="noreferrer" class="gmail_msg" target="_blank">https://gist.github.com/erica/db9ce92b3d23cb20799460f603c0ae7c</a><br class="gmail_msg">
>>><br class="gmail_msg">
>>> -- E<br class="gmail_msg">
>>><br class="gmail_msg">
>>><br class="gmail_msg">
>>> Introducing unwrap<br class="gmail_msg">
>>><br class="gmail_msg">
>>> • Proposal: TBD<br class="gmail_msg">
>>> • Author: Erica Sadun, Chris Lattner, David Goodine<br class="gmail_msg">
>>> • Status: TBD<br class="gmail_msg">
>>> • Review manager: TBD<br class="gmail_msg">
>>> Introduction<br class="gmail_msg">
>>><br class="gmail_msg">
>>> This proposal introduces unwrap, simplifying common shadowing and allowing a unified syntax for one-item associated values such as Result types.<br class="gmail_msg">
>>><br class="gmail_msg">
>>> Swift-evolution thread: guard let x = x<br class="gmail_msg">
>>><br class="gmail_msg">
>>> Motivation<br class="gmail_msg">
>>><br class="gmail_msg">
>>> 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="gmail_msg">
>>><br class="gmail_msg">
>>> Compare:<br class="gmail_msg">
>>><br class="gmail_msg">
>>> guard let foobar = foobar else { …<br class="gmail_msg">
>>> }<br class="gmail_msg">
>>><br class="gmail_msg">
>>> guard unwrap foobar else { … }<br class="gmail_msg">
>>> 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="gmail_msg">
>>><br class="gmail_msg">
>>> This syntax simplifies one-item associated value enumerations by offering a common syntax. Compare:<br class="gmail_msg">
>>><br class="gmail_msg">
>>> enum Result<T> { case success(T), error(Error<br class="gmail_msg">
>>> ) }<br class="gmail_msg">
>>><br class="gmail_msg">
>>><br class="gmail_msg">
>>> guard case let .success(value) = result else { ...<br class="gmail_msg">
>>> }<br class="gmail_msg">
>>><br class="gmail_msg">
>>> guard unwrap result else { ... }<br class="gmail_msg">
>>> In the latter case result is bound to the wrapped value. Again, it is simpler and clearer, even with non-Optional types.<br class="gmail_msg">
>>><br class="gmail_msg">
>>> Detailed Design<br class="gmail_msg">
>>><br class="gmail_msg">
>>> unwrap can be used with any one-value enumeration. The unwrapped value is bound to the same symbol as the associated type.<br class="gmail_msg">
>>><br class="gmail_msg">
>>> enum TypeName<T, U> { case anycase(T), anothercase(U) }<br class="gmail_msg">
>>><br class="gmail_msg">
>>> // First and second are type `TypeName`<br class="gmail_msg">
>>> let first = TypeName.anyCase(value1)<br class="gmail_msg">
>>> let second = TypeName. anothercase(value2)<br class="gmail_msg">
>>><br class="gmail_msg">
>>> guard unwrap first else { ... }<br class="gmail_msg">
>>> // first is now shadowed as type T<br class="gmail_msg">
>>><br class="gmail_msg">
>>> guard unwrap second else { ... }<br class="gmail_msg">
>>> // second is now shadowed as type U<br class="gmail_msg">
>>><br class="gmail_msg">
>>> Impact on Existing Code<br class="gmail_msg">
>>><br class="gmail_msg">
>>> This change is additive and has no impact on existing code other than intentional refactoring.<br class="gmail_msg">
>>><br class="gmail_msg">
>>> Timeline<br class="gmail_msg">
>>><br class="gmail_msg">
>>> This proposal is additive and not suited for consideration until Swift 4 phase 2<br class="gmail_msg">
>>><br class="gmail_msg">
>>> Alternatives Considered<br class="gmail_msg">
>>><br class="gmail_msg">
>>> • Using a bind keyword. Past discussions were held in the first week of February 2016.<br class="gmail_msg">
>>> • Fixing pattern matching grammar<br class="gmail_msg">
>>> • Not using this approach<br class="gmail_msg">
>>><br class="gmail_msg">
>>> _______________________________________________<br class="gmail_msg">
>>> swift-evolution mailing list<br class="gmail_msg">
>>> <a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
>>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
>><br class="gmail_msg">
>> _______________________________________________<br class="gmail_msg">
>> swift-evolution mailing list<br class="gmail_msg">
>> <a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
><br class="gmail_msg">
<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div>
</div></blockquote><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><span class="gmail_msg">_______________________________________________</span><br class="gmail_msg"><span class="gmail_msg">swift-evolution mailing list</span><br class="gmail_msg"><span class="gmail_msg"><a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a></span><br class="gmail_msg"><span class="gmail_msg"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class="gmail_msg"></div></blockquote></div></blockquote></div>
_______________________________________________<br class="gmail_msg">swift-evolution mailing list<br class="gmail_msg"><a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg"></div></blockquote></div><br class="gmail_msg"></div></blockquote></div>
</div></blockquote></div><br class=""></div></div></body></html>