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?
2 Likes
owenv
(Owen Voorhees)
2
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.
Yeah, pattern matching is sort of broken when you have the same base name: Update on SE-0110 and SE-0155
Jens
6
I don't think you file dups on Jira, you can press "vote" and "watch" on an issue though.
1 Like
BigSur
({ @MainActor in M1.Ultra }(Swift))
7
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
Q:
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...