Skip to content

Instantly share code, notes, and snippets.

@guidomb
Last active June 3, 2018 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guidomb/57b8ac391dfeb0514c9ae872b6574175 to your computer and use it in GitHub Desktop.
Save guidomb/57b8ac391dfeb0514c9ae872b6574175 to your computer and use it in GitHub Desktop.
Swift 4.2 regression
import Foundation
struct AnyError: Error {
private let error: Error
init(_ error: Error) {
self.error = error
}
}
enum Result<T, E: Error> {
case success(T)
case failure(E)
func mapError<NewError: Error>(_ transform: (E) -> NewError) -> Result<T, NewError> {
switch self {
case .success(let value):
return .success(value)
case .failure(let error):
return .failure(transform(error))
}
}
}
protocol Service {
func execute<T>() -> Result<T, AnyError>
}
struct Foo {
enum Bar: Error {
case foo
case bar(Error)
}
}
final class Baz {
func someFunction<T>(service: Service) -> Result<T, Foo.Bar> {
return service.execute().mapError(Foo.Bar.bar)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment