Because "guard" already means something...<br><br><br><div class="gmail_quote"><div dir="ltr">On Mon, Oct 31, 2016 at 13:45 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">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>