Swift pattern matching with protocol

Viewed 490

Don't know if it's a bug in compiler or there's something that I'n not aware of.

When matching against concrete types I can combine two cases like:

    enum SomeEnum {
        case a, b, c
    }

    let param: (SomeEnum, Any) = something
    switch (param) {
        case (.a, let param as Int),
             (.b, let param as Int):
            print("a or b with solid type (Int) \(param)")
        default: print("none of above")
    }

However, if I would like to match against protocol like:

    switch (param) {
        case (.a, let param as Equatable),
             (.b, let param as Equatable):
            print("a or b with protocol \(param)")
        default: print("none of above")
    }

I get Segmentation fault: 11 during compilation. Solution for this is to duplicate code like:

    switch (param) {
        case (.a, let param as Equatable):
            print("a or b with protocol \(param)")
         case (.b, let param as Equatable):
            print("a or b with protocol \(param)")
        default: print("none of above")
    }

Can someone tell me why it behaves like this?

1 Answers
Related