if case 1, 2, 3 = x { ... } results in error in swift 2


(Amir Michail) #1

But "if case 1...3 = x { ... }" works.

Why?


(Lily Ballard) #2

`if case 1, 2, 3 = x {` does not work because case statements can only
contain a single pattern when used in an if/for/while. Only when found
in `switch` statements can they contain multiple comma-separated
patterns.

`if case 1...3 = x {` works because this a single pattern `1...3`, and
it's equivalent to saying `if 1...3 ~= x {` (`~=` is the operator that
is used for doing value comparisons in patterns, and it's implemented
for all `IntervalType`s to call `interval.contains(x)`).

Also, this seems like a question that's more appropriate for swift-users
than swift-evolution.

···

On Thu, Dec 3, 2015, at 04:40 PM, Amir Michail wrote:

But "if case 1...3 = x { ... }" works.

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


(Amir Michail) #3

`if case 1, 2, 3 = x {` does not work because case statements can only
contain a single pattern when used in an if/for/while. Only when found
in `switch` statements can they contain multiple comma-separated
patterns.

But why aren’t multiple comma-separated patterns supported in if/for/while just like in switch statements?

I think they should be.

···

On Dec 3, 2015, at 7:51 PM, Kevin Ballard <kevin@sb.org> wrote:

`if case 1...3 = x {` works because this a single pattern `1...3`, and
it's equivalent to saying `if 1...3 ~= x {` (`~=` is the operator that
is used for doing value comparisons in patterns, and it's implemented
for all `IntervalType`s to call `interval.contains(x)`).

Also, this seems like a question that's more appropriate for swift-users
than swift-evolution.

On Thu, Dec 3, 2015, at 04:40 PM, Amir Michail wrote:

But "if case 1...3 = x { ... }" works.

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

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


(Adrian Kashivskyy) #4

IIRC multiple case patterns in `if-case` are not yet supported due to compiler limitations.

Pozdrawiam – Regards,
Adrian Kashivskyy

···

Wiadomość napisana przez Amir Michail <a.michail@me.com> w dniu 04.12.2015, o godz. 01:40:

But "if case 1...3 = x { ... }" works.

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


(Lily Ballard) #5

Ah, so you're proposing that the syntax be changed, rather than merely
asking why the code doesn't work? That makes sense.

As for why it's not supported now, I don't know. Maybe there's some
syntactic ambiguity in allowing it? Or maybe there was just concern
about it being confusing. Hopefully someone involved in the
implementation of it can answer this.

-Kevin Ballard

···

On Thu, Dec 3, 2015, at 04:55 PM, Amir Michail wrote:

> On Dec 3, 2015, at 7:51 PM, Kevin Ballard <kevin@sb.org> wrote:
>
> `if case 1, 2, 3 = x {` does not work because case statements can only
> contain a single pattern when used in an if/for/while. Only when found
> in `switch` statements can they contain multiple comma-separated
> patterns.

But why aren’t multiple comma-separated patterns supported in
if/for/while just like in switch statements?

I think they should be.

>
> `if case 1...3 = x {` works because this a single pattern `1...3`, and
> it's equivalent to saying `if 1...3 ~= x {` (`~=` is the operator that
> is used for doing value comparisons in patterns, and it's implemented
> for all `IntervalType`s to call `interval.contains(x)`).
>
> Also, this seems like a question that's more appropriate for swift-users
> than swift-evolution.
>
> On Thu, Dec 3, 2015, at 04:40 PM, Amir Michail wrote:
>> But "if case 1...3 = x { ... }" works.
>>
>> Why?
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution