[swift-users] Expression pattern cannot be a named tuple constant

Zhao Xin owenzx at gmail.com
Mon Jul 4 21:12:50 CDT 2016


I think switch treats (x,y) as two variables instead of a tuple. So it
prohibits int_1_1 as it looks like one value only at the first glance.
Below two expressions will work.

case (int_1_1.0,int_1_1.1):        // <<<<

case let foo where foo == int_1_1:        // <<<<

​That why you can use something like

case (_, 10)



​Zhaoxin ​


On Tue, Jul 5, 2016 at 5:56 AM, Neil Faiman via swift-users <
swift-users at swift.org> wrote:

> (Resending — this didn’t get any responses when I sent it a month ago.)
>
> Swift 2.2 in Xcode 7.3.1.
>
> Apparently you cannot use a named tuple constant as an expression pattern
> in a case label.
>
> func test(x: Int, y: Int) -> Int {
>    let int_1 = 1
>    switch x {
>    case 0:
>        return 0
>    case int_1:
>        return 1
>    default:
>        break
>    }
>
>    let int_1_1: (Int, Int) = (1, 1)
>    switch (x, y) {
>    case (0, 0):
>        return 0
>    case int_1_1:        // <<<<
>        return 1
>    default:
>        return -1
>    }
> }
>
> error: expression pattern of type '(Int, Int)' cannot match values of type
> '(Int, Int)'
>    case int_1_1:
>         ^~~~~~~
>
> The error message is particularly amusing.
>
>         - Neil Faiman
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160705/12d12085/attachment.html>


More information about the swift-users mailing list