[swift-evolution] Allow let binding of non-optionals

Jacob Bandes-Storch jtbandes at gmail.com
Thu Jan 7 13:14:58 CST 2016


I see I spoke to soon. Indeed there is a warning "'if' condition is always
true" when you use *only* "if case let x = nonOptional()" with no actual
conditions. That's good, but as you said, leaves us without a solution for
the original problem (in the case where you're not trying to mix it with
optionals).

As much as I think "do let" makes sense:

    do let x = somethingNonOptional() {
        // use x
    }

It's worth pointing out that this works today by just moving the brace
around:

    do { let x = somethingNonOptional()
        // use x
    }

Jacob Bandes-Storch

On Thu, Jan 7, 2016 at 11:08 AM, Jacob Bandes-Storch <jtbandes at gmail.com>
wrote:

> I think you might have misunderstood Joe's suggestion — it does work for
> me. Here's an example:
>
>     if let y = somethingOptional(),
>         case let x = somethingNonOptional()
>         where x != y
>     {
>         // Rejoice!
>     }
>
>
> Jacob Bandes-Storch
>
> On Thu, Jan 7, 2016 at 11:07 AM, Gwendal Roué <gwendal.roue at gmail.com>
> wrote:
>
>>
>> Le 7 janv. 2016 à 20:02, Joe Groff <jgroff at apple.com> a écrit :
>>
>>
>> On Jan 7, 2016, at 10:59 AM, Jacob Bandes-Storch via swift-evolution <
>> swift-evolution at swift.org> wrote:
>>
>>         *do let* value2 = somethingNonOptional()
>>
>>
>> 'case let' already works like this, by declaring a pattern 'let a' that
>> unconditionally matches anything.
>>
>> -Joe
>>
>>
>> Well, the compiler complains, and does not provide any nice suggestion.
>>
>> I, and most code I’ve seen so far (including StackOverflow), solve this
>> issue with an extra variable declared in the outer scope:
>>
>> // error
>> if let x = value...
>> // OK
>> let x = value
>> if …
>>
>> Remember the double role of `if let`: 1. unwrap the optional, 2. define a
>> new variable, scoped to the `if` statement. We have 1, but not 2, and this
>> is the gist of this thread.
>>
>> Gwendal
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160107/414f0897/attachment.html>


More information about the swift-evolution mailing list