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

Can we have an eitherReader convenience function? #48

Closed
nh2 opened this issue Jun 19, 2013 · 5 comments
Closed

Can we have an eitherReader convenience function? #48

nh2 opened this issue Jun 19, 2013 · 5 comments

Comments

@nh2
Copy link
Contributor

nh2 commented Jun 19, 2013

For parsing simple arguments, I often define functions of type parseFoo :: String -> Either String a, which are used in multiple places, and then I want to use them with reader.

Could we have a convenience function like this?

eitherReader str2either = reader (either (Left . ErrorMsg) Right . str2either)

This would not be necessary if the fail instance of Either String was not an exception, but it is and fail isn't a realy thing in Monad either (no pun intended).

Thanks :)

@pcapriotti
Copy link
Owner

Yes, that would be a useful addition. Can you submit a patch?

@nh2
Copy link
Contributor Author

nh2 commented Jun 19, 2013

I just realized that my implementation doesn't work.

Weirdly enough, it does work on a successful parse; if the parse fails, however, and returns Left, then my program just exits with status one instead of printing the error message.

This implementation, using fail from the Monad instance, works as expected in the case of failure:

eitherReader str2either = reader (either fail return . str2either)

It's very unclear to me what's going on here, I believe the two should really do the same.

(This is like in #47, but this time, it's on the 0.5.2.1, not on master.)

@pcapriotti
Copy link
Owner

I'll look into it, thanks for the report.

@nh2
Copy link
Contributor Author

nh2 commented Jun 19, 2013

I think it comes from the

(Just r, ErrorMsg _) -> return r

here (at least the error message I expect is contained in that.

@nh2
Copy link
Contributor Author

nh2 commented Oct 12, 2013

Thanks!

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

No branches or pull requests

2 participants