Does pattern matching allow labels

This thread (Making use of enum cases with a common base name) mentions about enums with the same base name:

enum SyntaxTree {
   case type(variables: [TypeVariable])
   case type(instantiated: [Type])

It makes me realise you can't do this:

if case let .type(variables: test) = ... { }
if case let .type(instantiated: test) = ... { }

It always force me into one of them.
I feel like you should have always been able to do this.
Is this a regression, or just a Mandela effect?


I'm not sure if this has ever worked, but I'd say it's definitely bug-worthy. I ran a quick test and which cases are allowed appears to depend on their declaration order in the enum.

Ok, filed: [SR-12764] Pattern matching forces arbitrary label · Issue #55209 · apple/swift · GitHub.

1 Like

Yeah, pattern matching is sort of broken when you have the same base name: Update on SE-0110 and SE-0155

Ah, ok, it looks like [SR-12229] Assertion failure when trying to pattern match enum cases with same base name · Issue #54655 · apple/swift · GitHub. How do I file a dup?

I don't think you file dups on Jira, you can press "vote" and "watch" on an issue though.

1 Like

Pretty wired. Actually pattern match allow labels but only the last one case pattern allowed.

enum E
    case P(s:String)
    case P(i:Int)
    case P(b:Bool) //only this - the last case can be matched and also be labelled matched
/* labelled pattern match */
if case let E.P(b:q)=E.P(b:true) {print(q)} 
//label `b` is allowed and print `true` - pattern matched

if case let E.P(s:q)=E.P(s:"blah") {print(q)} 
//error: `Tuple pattern element label 's' must be 'b'`

if case let E.P(i:q)=E.P(i:123) {print(q)} 
//error: `Tuple pattern element label 'i' must be 'b'`

/* unlabelled pattern match */
if case let E.P(q)=E.P(b:true) {print(q)} 
//print `true` - pattern matched

if case let E.P(q)=E.P(s:"blah") {print(q)}
//print nothing - pattern unmatched

if case let E.P(q)=E.P(i:123) {print(q)} 
//print nothing - pattern unmatched

1.Why only the last pattern case with label E.P(b:) is allowed in pattern match if statement? Others compile error...

2.Why only the last pattern case E.P(b:) can be matched? Others compiles but unmatched...