[swift-evolution] guard let x = x

Erica Sadun erica at ericasadun.com
Tue Nov 1 17:13:09 CDT 2016


> On Oct 31, 2016, at 7:37 PM, Joe Groff <jgroff at apple.com> wrote:
> 
> 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.)
> 
> -Joe

Feels like too much magic. I'd like there to be a positive declaration of intent.

The tl;dr of what I posted today is:

guard unwrap anOptional else { ... } // basic optional
if unwrap .string(myString) ~= jsonObject { ... } // Non-optional object
if unwrap .contact(code, var message) ~= jsonObject { ... } // Multi-valued

if .some(let value) ~= anOptional { ... } // Pattern match operator instead of case
if let value? ~= anOptional { ... } // ditto

-- E


> 
>> On Oct 28, 2016, at 3:34 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> 
>>> On Oct 26, 2016, at 11:39 AM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> 
>>>> On Oct 26, 2016, at 10:23 AM, Joshua Alvarado <alvaradojoshua0 at gmail.com> wrote:
>>>> 
>>>> In your example the keyword only makes sense if you are shadowing the optional variable. How would unwrap work with a different name?
>>> 
>>> It wouldn’t: “unwrap” would never include an equal sign.  If you want to do that, use a standard "if let”.
>>> 
>>> -Chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161101/4339c2ae/attachment.html>


More information about the swift-evolution mailing list