[swift-evolution] [Idea] Remove optional pattern binding

Антон Жилин antonyzhilin at gmail.com
Mon May 2 05:56:41 CDT 2016


Currently,

if case let x? = y { ... }

is a sugared version of:

if case let .some(x) = y { ... }

which is in turn a sugared version of:

if case .some(let x) = y { ... }

Pattern matching in `if` works like this: left hand side of `=` contains
supposed structure of value at right hand side.
If we omit `case`, we get:

if let x? = y { ... }

Another example: checks if tuple contains two `.some`:

if let (x?, y?) = z { ... }

- Anton

2016-05-02 13:25 GMT+03:00 Haravikk <swift-evolution at haravikk.me>:

>
> On 1 May 2016, at 09:12, Антон Жилин <antonyzhilin at gmail.com> wrote:
>
> Pattern binding for optionals will look like:
>
> if let x? = y { … }
>
>
> Would take a little getting used to, but I think I’d be fine with it, as
> the way things are now is inconsistent with regular assignments (which
> remain optional).
>
> My only concern is why the question mark on the left hand side? I don’t
> really have any concrete reason against it, but it just feels kind of odd
> to me, I’m just hoping for some reasoning why some of the alternatives
> aren’t better fits like:
>
> if let x = y? { … } // More like optional chaining, which is familiar as
> an “if non-nil proceed” behaviour
> if let x ?= y { … } // More obviously a special type of assignment that
> could fail if y is nil
>
> Again, I’m in favour, I’m just curious why the mentioned format, were
> others considered previously, or is just because that’s how the
> case-keyword form does it? (I don’t use it as I’ve never liked that form,
> and it doesn’t seem that well known anyway).
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160502/fe43b5a0/attachment.html>


More information about the swift-evolution mailing list