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 tryMap. Like flatMap but for Swift2 error handling. #114

Closed
wants to merge 2 commits into from

Conversation

bwhiteley
Copy link
Contributor

The tryMap function allows code that throws to be more easily wrapped.

/// This is a synonym for `tryMap`.
public func >>- <T, U, Error> (result: Result<T, Error>, @noescape transform: T throws -> U) -> Result<U, Error> {
return result.tryMap(transform)
}
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 keen on overloading >>- to mean two different things.

Copy link
Member

Choose a reason for hiding this comment

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

Yea, this should be limited to flatMap

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@robrix @neilpa is there a different operator you recommend?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t know of an operator that has the appropriate semantics, and I’d rather not invent one. A method without an operator is totally acceptable.

Copy link
Member

Choose a reason for hiding this comment

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

A method without an operator is totally acceptable.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@robrix @neilpa I agree it's good to introduce new operators sparingly. The newly proposed map behaves a lot like flatMap. The problem is that you can't currently mix and match >>- with flatMap or the newly proposed map. I wonder if this can be fixed by adjusting the precedence of >>-.

Copy link
Member

Choose a reason for hiding this comment

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

Personally, I'd like to see >>- removed and not expose any operators.

@robrix
Copy link
Contributor

robrix commented Nov 3, 2015

Thanks for the PR!

do {
return .Success(try transform(value))
}
catch {
Copy link
Member

Choose a reason for hiding this comment

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

For consistency I'd say that catch should be on the same line as the closing brace of do.

@robrix
Copy link
Contributor

robrix commented Nov 4, 2015

Maybe it’d be better to change map and/or flatMap to accommodate throws instead. Typically these would be written using throws and rethrows, but in our case they’d catch instead of rethrowing.

@antitypical/result: Any thoughts?

@bwhiteley
Copy link
Contributor Author

Closing this and opened #118 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants