Skip to content
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

Merged
merged 6 commits into from
Nov 29, 2016
Merged

Conversation

ikesyo
Copy link
Member

@ikesyo ikesyo commented Nov 14, 2016

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.

This is an additive, non-breaking change.

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.
@ikesyo ikesyo force-pushed the type-erased-anyerror branch from 9abb873 to a7d0cdc Compare November 14, 2016 13:51
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")
Copy link
Member Author

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.

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?

Copy link
Member Author

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
Copy link
Contributor

@norio-nomura norio-nomura Nov 15, 2016

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.

Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Member Author

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).

Copy link
Contributor

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 :shipit:

Copy link
Member Author

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
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 1a12328.

@ikesyo
Copy link
Member Author

ikesyo commented Nov 28, 2016

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 {
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do it!

Copy link
Member Author

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@ikesyo
Copy link
Member Author

ikesyo commented Nov 28, 2016

Thanks for the review comments! I'll merge this once CI passes, then I'd like to cut a new release with #201.

@ikesyo ikesyo modified the milestone: 3.1 Nov 28, 2016
@NachoSoto NachoSoto merged commit a1065e3 into master Nov 29, 2016
@NachoSoto NachoSoto deleted the type-erased-anyerror branch November 29, 2016 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants