//Suppose you have an enum like this:
enum MessageType {
case request(string: String)
case request(double: Double)
}
//make an instance
var a = MessageType.request(double: 0.1)
//here comes the fun part: match it!
//it seems like it cannot do it
//because the compiler says that the second case is already handled
//by the first one
switch a {
case let .request(string): print(string)
case let .request(double): print(double)
}
Does this mean that even though it is possible to declare two cases with the same names, but different signatures, it is not possible to match them? What am I doing wrong, if that is not the case?
BigSur
({ @MainActor in M1.Ultra }(Mac Studio))
#2
Yep, that's a bug or undefined behavior. The warning should be suppressed .
You can even declare an Enum as
enum MessageType {
case request(label: T)
case request(anotherLabel: T)
}
I'm pretty sure that's reasonably ambiguous as nothing tells the compiler what's different about the string and double variable bindings. Try adding type annotations:
switch a {
case let .request(string as String): print(string)
case let .request(double as Double): print(double)
}
@total_swiftification Yes that seems like a bug in memberLookup, where seems like is failing to match the declarations for the "ambiguous" case. I would suggest create a ticket on https://bugs.swift.org :)
Oh my, the oldest report of that issue happened in 2018th. Maybe after waiting for 2 more years this 'omissible' problem will happen to have its resolution.
SR-12206 should be fixed by my PR very soon (i just need to figure out this one last thing), after that I can take a look at SR-12229. I’m hoping to get SE-0155 fully implemented within the next few months🤞