Proposal: Optional Binding Shorthand Syntax


(Tyler Mandry) #1

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? :slight_smile:

···

--
Tyler Mandry


(Chris Lattner) #2

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?


(Alex Lew) #3

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? :slight_smile:

--
Tyler Mandry

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


(Tyler Mandry) #4

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


(Chris Lattner) #5

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


(Tyler Mandry) #6

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:
https://github.com/apple/swift/commit/20f8f09ea8de5eb1c0cb559e59c8a8e8a0e115a9#diff-4513b692cdd5476630ebb66b73d5bf4b
.

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? :slight_smile:

--
Tyler Mandry

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

--
Tyler Mandry