Proposal: Optional Binding Shorthand Syntax

What isn't clear to me from Chris's or John's comments is why the `if let
x? = foo` syntax was taken away. Was it for backward-compatibility? To me,
this syntax does have special syntactic support, and also seems to make it
clearer what's going on. It's also analogous to Swift's type inference for
generic <T?> types, for example:

   func doSomething<T? where T: Equatable>(x: T?) -> T { ... }

Note the T? in brackets. If I pass an Int? to doSomething, the type is
"unwrapped" and T becomes Int. It seems like `if let x? = foo` would follow
the same pattern.

Was the syntax taken away for reasons unlikely to change, or is it up for
discussion? :)

···

--
Tyler Mandry

FWIW, that syntax hasn’t been taken away, it just means something different now.

  if let x = foo() {

captures is privileged syntax for testing an optional and binding to the thing inside of it. The patch I linked to moved away from this, but at great cost: it uglified lots of common code. Swift privileges optional in a number of ways (e.g. it is the result of as?, try?, etc) and the standard library uses it pervasively as well. Optimizing for that common case makes sense.

-Chris

···

On Dec 6, 2015, at 1:38 PM, Tyler Mandry via swift-evolution <swift-evolution@swift.org> wrote:

What isn't clear to me from Chris's or John's comments is why the `if let x? = foo` syntax was taken away. Was it for backward-compatibility?

let a: Int? = 3
if case let b? = a {
    print(b)
}

works for me in Swift 2.2. Have they announced that they're taking this
away?

···

On Sun, Dec 6, 2015 at 4:38 PM, Tyler Mandry via swift-evolution < swift-evolution@swift.org> wrote:

What isn't clear to me from Chris's or John's comments is why the `if let
x? = foo` syntax was taken away. Was it for backward-compatibility? To me,
this syntax does have special syntactic support, and also seems to make it
clearer what's going on. It's also analogous to Swift's type inference for
generic <T?> types, for example:

   func doSomething<T? where T: Equatable>(x: T?) -> T { ... }

Note the T? in brackets. If I pass an Int? to doSomething, the type is
"unwrapped" and T becomes Int. It seems like `if let x? = foo` would follow
the same pattern.

Was the syntax taken away for reasons unlikely to change, or is it up for
discussion? :)

--
Tyler Mandry

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

True, I meant why was the `?` taken out of the syntax. I suppose it can
feel redundant to have `if x? = try? foo()` or similar, in the cases you
mentioned. That said, in the common case of unwrapping a variable of
optional type (`let foo: Int?`) which is not written with a `?`, it can add
extra clarity.

···

On Sun, Dec 6, 2015 at 4:42 PM, Chris Lattner <clattner@apple.com> wrote:

> On Dec 6, 2015, at 1:38 PM, Tyler Mandry via swift-evolution < > swift-evolution@swift.org> wrote:
>
> What isn't clear to me from Chris's or John's comments is why the `if
let x? = foo` syntax was taken away. Was it for backward-compatibility?

FWIW, that syntax hasn’t been taken away, it just means something
different now.

        if let x = foo() {

captures is privileged syntax for testing an optional and binding to the
thing inside of it. The patch I linked to moved away from this, but at
great cost: it uglified lots of common code. Swift privileges optional in
a number of ways (e.g. it is the result of as?, try?, etc) and the standard
library uses it pervasively as well. Optimizing for that common case makes
sense.

-Chris

--
Tyler Mandry

Swift 2 does support ? patterns.

-Chris

···

On Dec 6, 2015, at 2:48 PM, Tyler Mandry <tmandry@gmail.com> wrote:

True, I meant why was the `?` taken out of the syntax

Ah no, it's just that they at one time had support for taking away `case`
and doing `if let b? = a`. See the commit notes that Chris linked to here:

.

I guess the current `if let b = a` syntax is what John was referring to as
special syntactic support, but I still don't see why the `?` was taken out
of the syntax.

···

On Sun, Dec 6, 2015 at 3:43 PM, Alex Lew <alexl.mail+swift@gmail.com> wrote:

let a: Int? = 3
if case let b? = a {
    print(b)
}

works for me in Swift 2.2. Have they announced that they're taking this
away?

On Sun, Dec 6, 2015 at 4:38 PM, Tyler Mandry via swift-evolution < > swift-evolution@swift.org> wrote:

What isn't clear to me from Chris's or John's comments is why the `if let
x? = foo` syntax was taken away. Was it for backward-compatibility? To me,
this syntax does have special syntactic support, and also seems to make it
clearer what's going on. It's also analogous to Swift's type inference for
generic <T?> types, for example:

   func doSomething<T? where T: Equatable>(x: T?) -> T { ... }

Note the T? in brackets. If I pass an Int? to doSomething, the type is
"unwrapped" and T becomes Int. It seems like `if let x? = foo` would follow
the same pattern.

Was the syntax taken away for reasons unlikely to change, or is it up for
discussion? :)

--
Tyler Mandry

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Tyler Mandry