-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add type-erased AnyError
for generic contexts
#198
Conversation
In Swift 3, `Error` should be widely used instead of `NSError`. So the type should be useful to wrap arbitrary `Error` instances for uses in generic contexts.
9abb873
to
a7d0cdc
Compare
public func materialize<T>(_ f: () throws -> T) -> Result<T, NSError> { | ||
return materialize(try f()) | ||
} | ||
|
||
@available(*, deprecated, message: "Use the overroad which returns `Result<T, AnyError>` instead") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not confident whether those should be deprecated or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't it over'l'oad?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😱 Thank you!
|
||
public static func ==(lhs: AnyError, rhs: AnyError) -> Bool { | ||
return lhs.error._code == rhs.error._code | ||
&& lhs.error._domain == rhs.error._domain |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_domain
should be compared before _code
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I withdraw my comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick question: Are _domain
and _code
allowed to use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are
_domain
and_code
allowed to use?
I'm not confident. It should be okay to remove Equatable
conformance (in that case use them for testing purpose only).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not confident. It should be okay to remove Equatable conformance (in that case use them for testing purpose only).
+1. I also think removing Equatable
is safer to ship
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 3cafe3e.
public let error: Swift.Error | ||
|
||
public init(_ error: Swift.Error) { | ||
self.error = error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need to check if error
is AnyError
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 1a12328.
Any other thoughts? @antitypical/result |
@@ -169,6 +183,29 @@ extension NSError: ErrorProtocolConvertible { | |||
/// contains an `Int`eger and is guaranteed never to be a `failure`. | |||
public enum NoError: Swift.Error { } | |||
|
|||
/// A type-erased error which wraps an arbitrary error instance. This should be | |||
/// useful for generic contexts. | |||
public struct AnyError: Swift.Error, ErrorProtocolConvertible, CustomStringConvertible { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe separate the conformances from the type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in aac425d
public let error: Swift.Error | ||
|
||
public init(_ error: Swift.Error) { | ||
if let anyError = error as? AnyError { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
Thanks for the review comments! I'll merge this once CI passes, then I'd like to cut a new release with #201. |
In Swift 3,
Error
should be widely used instead ofNSError
. So the type should be useful to wrap arbitraryError
instances for uses in generic contexts.This is an additive, non-breaking change.