New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-1121] Custom pattern matching with enum types in Swift 2.2 not working as expected #43734
Comments
Ah, yeah, when we see an enum case we short-circuit into Pattern Mode rather than Expression Mode for switch case matching. |
Just came across this in Xcode 9.2. Example: enum APIRoute {
case foo
case bar
}
extension APIRoute {
static func ~= (pattern: APIRoute, value: URL) -> Bool {
// ...
}
}
switch url {
case APIRoute.foo: // error: enum case 'foo' is not a member of type 'URL'
// ...
} |
More specifically, I'm using it in an optional context: let response: HTTPURLResponse
switch response.url { // of type URL?
case APIRoute.foo?:
// ...
} |
Stupid workaround: make the case not look like a pattern:
|
Ha, wow, I did not know that was legal. I ended up using passing the value through an identity function named case pattern(APIRoute.foo)?: |
Comment by Sergey Galezdinov (JIRA) @belkadan, any ETA on this? facing this on 9.3 with swift 4.1 |
@belkadan , @xedin , Zaphod (JIRA User) I can also reproduce the error in a Playground using Swift 4.2 and Xcode version 10.1 (10B61). enum A: Int {
case zero
case one
static func ~= (pattern: B, value: A) -> Bool {
return pattern.rawValue == value.rawValue
}
}
enum B: Int {
case one = 1
case two
}
switch A.one {
case B.one: print("1")
default: break
} Produces the error message "Enum case 'one' is not a member of type 'A'" The suggested workaround by @belkadan using `.self` still works. The following switch statement compiles without a problem. switch A.one {
case B.one.self: print("1")
default: break
} @belkadan & @xedin is there any update regarding this bug? Is there any way I can help with resolving this issue? |
Sorry I was unable to get to this and probably wouldn't be able for some time still, because of amount of diagnostic work I need to handle... Code contributions are always welcome if you up to try and figure out what is going on here, I can help you out with suggestions etc.! |
@xedin Thanks for your response! |
I really would like to think so! It seems like a good place to start would be to figure out how and when `~=` is generated implicitly for case statements. |
Looks like it happens in TypeChecker::coercePatternToType, just need to figure out how to make it so `B.one` could considered expression pattern I think if it didn't match otherwise? |
@xedin Thanks for the input. I will take a look at the code and issue next week, sounds like a great challenge for me. |
Sounds good! |
Comment by Mikhail Isaev (JIRA) Will it ever be fixed? We're waiting three years already 🙁 |
Hi mikeisaev (JIRA User), I took a look at the problem a few months ago but was a bit overwhelmed with the complexity. I learned a lot in the last months and would like to retake a look at it as soon as I have a bit more time around August. |
Comment by André Huyghues-Beaufond (JIRA) Hello, I just found this old issue. It is still open but this now compiles : import Foundation
enum LineType : String {
case Event = "event:"
case Data = "data:"
}
extension String {
func isOfType(type: LineType) -> Bool {
return self.hasPrefix(type.rawValue)
}
}
func ~= (pattern: LineType, value: String) -> Bool {
return value.isOfType(type: pattern)
}
let testLine = "event:yada-yada-yada"
switch testLine {
case _ where testLine.count == 0:
print("Empty")
case .Event:
print("Event") // "Event\n"
case .Data:
print("Data")
default:
print("Unknown Type")
} Does this mean that this bug is now fixed ? |
Environment
Xcode 7.3 (7D175)
Additional Detail from JIRA
md5: b3520825d1f44d2b00094ba8bbb81280
relates to:
Issue Description:
When using the ~= operator to create custom pattern matching with an enum type, the compiler raises an error instead of calling the righteous function.
Steps to Reproduce:
Example of code not compiling, copy and paste it in a playground:
The text was updated successfully, but these errors were encountered: