-
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 a version of map
that converts thrown errors to Failure
s.
#118
Changes from 5 commits
e2fa2bb
1e277d8
dee4350
329b819
855a5d7
2e5749a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,30 @@ public extension ResultType { | |
} | ||
} | ||
|
||
/// Protocol used to constrain `tryMap` to `Result`s with compatible `Error`s. | ||
public protocol ErrorTypeConvertible: ErrorType { | ||
typealias ConvertibleType = Self | ||
static func errorFromErrorType(error: ErrorType) -> ConvertibleType | ||
} | ||
|
||
public extension ResultType where Error: ErrorTypeConvertible { | ||
|
||
/// Returns the result of applying `transform` to `Success`es’ values, or wrapping thrown errors. | ||
public func tryMap<U>(@noescape transform: Value throws -> U) -> Result<U, Error> { | ||
return flatMap { value in | ||
do { | ||
return .Success(try transform(value)) | ||
} | ||
catch { | ||
guard let convertedError = Error.errorFromErrorType(error) as? Error else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused about this, if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You would think so, but
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That seems like a bug. public protocol ErrorTypeConvertible: ErrorType {
static func errorFromErrorType(error: ErrorType) -> Self
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That does permit the use of a plain I boiled it down to a sample that you can tinker with in a Playground.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably related to https://twitter.com/jckarter/status/672931114944696321. Given that the cast can never fail I'd just use |
||
fatalError("Unable to convert error type '\(error.dynamicType)' to type '\(Error.self)'") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool |
||
} | ||
return .Failure(convertedError) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this actually behaves like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, and would have preferred to call it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. /cc @antitypical/result There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spitballing, but: Is there maybe a way to rewrite the current There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so. The return type of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I don’t think this is precisely There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I originally called it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that was my bad, my apologies. I’m 👍 on the name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I renamed it to |
||
} | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Operators | ||
|
||
infix operator &&& { | ||
|
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.
Is this necessary? Maybe just use
Self
?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.
It is necessary because NSError is not final. From what I've seen, this is a common pattern.