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

Gwendal Roué gwendal.roue at gmail.com
Thu Jan 7 13:03:51 CST 2016


That would be fine for me, as long as Xcode suggests adding the missing `do`, or replacing `if` with `do` when I forget one!

Gwendal

> Le 7 janv. 2016 à 19:59, Jacob Bandes-Storch <jtbandes at gmail.com> a écrit :
> 
> Well, I don't think it should be universally allowed.
> 
> Consider:
> 
>     if let value = getValue() {
>         // do something with value
>     }
> 
> If getValue() returns an optional, this is fine. But if it's non-optional, then you've introduced an "if" when there really is no conditional control flow happening. This should still be a compiler error.
> 
> It gets trickier when you mix optional and non-optional values:
> 
>     if let value1 = somethingOptional(),
>         let value2 = somethingNonOptional()
>     {
>         // should this be allowed?
>     }
> 
> 
>     if let value1 = somethingNonOptional(),
>         let value2 = somethingOptional()
>     {
>         // How about this?
>     }
> 
> Here's an alternative idea: allow non-optional bindings in "do" blocks:
> 
>     do let value = somethingNonOptional() {
>         // use value
>     } // value is out of scope now
> 
> 
> And perhaps you could combine them with if-statements:
> 
>     if let value1 = somethingOptional(),
>         do let value2 = somethingNonOptional()
>         where value2 < value1
>     {
>         // use the values
>     }
> 
> 
> Jacob Bandes-Storch
> 
> On Thu, Jan 7, 2016 at 10:41 AM, Gwendal Roué <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> +1.
> 
> `if let` does two things: 1. unwrap the optional, 2. define a new variable, scoped to the `if` statement.
> 
> Sometimes we don’t need the unwrapping, but we’d like the new, temporary, scoped variable.
> 
> Gwendal
> 
> 
> > Le 7 janv. 2016 à 07:40, Russ Bishop via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> a écrit :
> >
> > I often want to bind variables to a specific scope, or perhaps I have three optionals I want to bind and one non-optional. (Often I need that non-optional value as part of a where condition check but not being able to bind it means I leak the variable to the outer scope).
> >
> > Not being able to bind non-optionals breaks the flow of the code and forces me to make a minor context switch when it really doesn’t matter, nor does it aid code clarity.
> >
> >
> > Does anyone have strong thoughts about this? I tried searching the evolution repo and the mailing list and didn’t see anything.
> >
> >
> > —russ
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> > https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 

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


More information about the swift-evolution mailing list