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

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


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/e93f4534/attachment.html>


More information about the swift-evolution mailing list