How can I access an enum value for a specific case without having to implement an enum function for each case?
I'm thinking something like this:
enum Result<S, F> {
case success(S)
case failure(F)
}
let resultSuccess = Result<Int, String>.success(1)
let resultFailure = Result<Int, String>.failure("Error")
let error: String? = case let .failure(error) = resultFailure
Something like this would also be acceptable:
let error: String? = (case let .failure(error) = resultFailure) ? error : ""
Obviously this doesn't even compile, but that's the gist of what I want.
Is that possible?
EDIT: explaining why my question is not the same as Extract associated value of enum case into a tuple
It's very simple, that answer access the values inside the if
statement, rather than as an Optional
like in my question. To specify a little more, imagine I have to extract values from multiple enums, and maybe enums inside enums, for that I'd need nested if
's, or guard
's, but with guard
I wouldn't be able to continue the flow, since it forces a return
.