I have the following bit of code in a macOS Playground in Xcode 13.2.1:
import Cocoa
enum Mode: Equatable {
case submitting(listenerUUID: UUID)
case success(response: Data)
case error
static func ==(lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case (.submitting, .submitting), (.success, .success), (.error, .error): return true
default: return false
}
}
}
let mode = Mode.success(response: Data())
print(String(format: "Is it a success? %@", (mode == .success ? "Yes" : "No")))
When I run it, I get the following error:
error: FallthroughSwitch.playground:18:45: error: cannot convert value of type 'Mode' to expected argument type 'DispatchTimeoutResult'
print(String(format: "Is it a success? %@", mode == .success ? "Yes" : "No"))
^
The error is appalling, but it appears to be a failure of type inference.
You can't write mode == .success because .success is not a valid value of Mode. Despite the fact that you've written your equatable implementation to consider all Mode values with case success as equal, you still have to provide a response in order to construct such a Mode. Try:
print(String(format: "Is it a success? %@", mode == .success(response: Data()) ? "Yes" : "No"))
More broadly, you probably don't want to mangle Equatable in this way. You probably would be better served by having:
extension Mode {
var isSuccess: Bool {
if case .success = self { return true }
else { return false }
}
}
@lukasa Yeah that isSuccess computed variable was my first preference. I just wanted to see if there was a more compact way. And then I hit on that weird error message. I do like your formatting, though. Compact enough for my purposes.