Late to the party, but I'd personally like to treat a case as it's own type that has an "is a" relationship to the parent enum type. Because it mirrors the class vs subclass relationship, we already have syntax for different use cases. For example, here's some objects that I've defined for network responses.
enum NetworkResult {
case success(response: NetworkResponse)
case failure(reason: FailureReason)
}
enum FailureReason {
case non200Response(response: NetworkResponse)
case noAuthToken
case noInternetConnection
case unknown(error: Error)
}
struct NetworkResponse {
var statusCode: Int
var headerFields: [String: String]
var body: String
}
These are super useful things to have as enums because the compiler forces devs on my team to handle both the success and failure case when they get a network response, and potentially handle different types of failures differently.
To me, a .success is a type of NetworkResult. A .noInternetConnection is a type of FailureResponse. Reflecting how we use classes and subclasses, I think a case should be a Metatype.
Enum
FailureResponse
EnumCase
FailureResponse.noInternetConnection (this *is* a FailureResponse)
This makes writing a lot of my code much more natural.
if let result as? NetworkResult.success(let response) where response.statusCode == 200 {
// Unlike synthesis, the compiler can now reflect that we already know 100% what this case is and avoid optionals
result.response.headerFields.forEach { print(\($0):\($1)) }
}
switch networkResult {
// Unlike the current situation we can evaluate case as a bool without caring about associated values
case failure(let reason) where !(failureReason is .unknown):
showErrorMessage(reason: reason)
}
I think the synthesis is fine, and I've been for it in the past mostly because it seems like changing enums at this level would delay ABI stability and the core team seemed against it, but I think using syntax we already have makes working with enums a lot nicer. As a side note, I agree with Xiaodi that we should get rid of enums with the same case name but different associated values. I don't see any reasonable use case for that.