The hidden bugs arise when, instead of pill, you invoke pill(), and the return value of pill() changes from Bool to Bool? or vice versa between one version of a library and another.<br><br>Point is, a second keyword is needed precisely because unwrapping should never be implicit. That much has been made clear in previous conversations where "guard let x" has been repeatedly rejected as a sugar for "guard let x = x". It stands to reason that "guard x" is even less acceptable.<br><br><div class="gmail_quote"><div dir="ltr">On Mon, Oct 31, 2016 at 14:43 Kenny Leung via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Now that I think about it some more, I think my proposal does not hurt the guard boolean case, and is still backward compatible.<br class="gmail_msg">
<br class="gmail_msg">
Current Universe:<br class="gmail_msg">
<br class="gmail_msg">
// This is the case Xiaodi mentions<br class="gmail_msg">
var pill :Boolean<br class="gmail_msg">
guard pill else {}<br class="gmail_msg">
<br class="gmail_msg">
// If pill was an optional-boolean, you would have to write it this way:<br class="gmail_msg">
var pill :Boolean?<br class="gmail_msg">
guard let pill = pill, pill else {}<br class="gmail_msg">
<br class="gmail_msg">
Kenny’s Universe:<br class="gmail_msg">
<br class="gmail_msg">
// This is the case Xiaodi mentions - nothing changes<br class="gmail_msg">
var pill :Boolean<br class="gmail_msg">
guard pill else{}<br class="gmail_msg">
<br class="gmail_msg">
// If pill was an optional-boolean, you might write this, but it only checks if pill is nil or not, and NOT true or false. This would be illegal in the Current Universe, so we know there is no backward incompatibility or hidden bugs.<br class="gmail_msg">
var pill :Boolean?<br class="gmail_msg">
guard pill else {}<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 12:33 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">
> I use guard pretty much exclusively in the “guard let x = x” context. I still think it’s a bad idea to sacrifice the 99% case for the 1% case.<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 12:16 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">
>> 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="gmail_msg">
>><br class="gmail_msg">
>> Meanwhile, "guard foo" tests if foo evaluates to true. An additive proposal cannot break this syntax.<br class="gmail_msg">
>> On Mon, Oct 31, 2016 at 14:11 Charlie Monroe <<a href="mailto:charlie@charliemonroe.net" class="gmail_msg" target="_blank">charlie@charliemonroe.net</a>> wrote:<br class="gmail_msg">
>> To me, it makes more sense to use<br class="gmail_msg">
>><br class="gmail_msg">
>> guard nonnil x else ...<br class="gmail_msg">
>><br class="gmail_msg">
>> since guard is guarding a condition is true - or am I wrong? Generally, it would tell the compiler that<br class="gmail_msg">
>><br class="gmail_msg">
>> guard x != nil else ...<br class="gmail_msg">
>><br class="gmail_msg">
>> which to me reads like guard nonnil x...<br class="gmail_msg">
>><br class="gmail_msg">
>> Just a side-note...<br class="gmail_msg">
>><br 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:<br class="gmail_msg">
>>><br 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">
>>> 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">
>>> 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.<br class="gmail_msg">
>>><br class="gmail_msg">
>>> Alvarado, Joshua<br 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">
>>>> Because "guard" already means something...<br class="gmail_msg">
>>>><br class="gmail_msg">
>>>><br 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">
>>>> 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">
>>>> _______________________________________________<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">
>>> 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">
> 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">
</blockquote></div>