Skip to content

Instantly share code, notes, and snippets.

@stleamist
Created March 22, 2021 15:07
Show Gist options
  • Save stleamist/5c666654cc3620c29f0be24a66ad4ad5 to your computer and use it in GitHub Desktop.
Save stleamist/5c666654cc3620c29f0be24a66ad4ad5 to your computer and use it in GitHub Desktop.
import Swift
extension Result {
func casting<NewSuccess, NewFailure>() -> Result<NewSuccess, NewFailure>? {
switch self {
case .success(let success as NewSuccess) where Failure.self is NewFailure.Type: return .success(success)
case .failure(let failure as NewFailure) where Success.self is NewSuccess.Type: return .failure(failure)
default: return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment