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 attempt to Either #189

Closed
wants to merge 1 commit into from
Closed

Add attempt to Either #189

wants to merge 1 commit into from

Conversation

jleider
Copy link
Contributor

@jleider jleider commented Oct 22, 2018

This adds a convenience function to Either that will produce an Either<Error|E, V> where the left is the result from a try/catch and the right is the result of a fn: () => V.

Since pretty much any type of thing can be thrown in JS the type cant be locked down to just Either<Error, V>. However, adding the union of Error|E will provide at least a little bit of type help to TS users.

Prior art comes from fromTryCatchNonFatal (recently renamed to attempt in v7.3) of ScalaZ disjunctions which are simply right biased Eithers just as in monet.js.

I am happy to change the function name to something like fromTryCatch if that makes it clearer as to what it does.

@@ -249,6 +249,7 @@ interface IEitherStatic extends IMonadStatic {
unit: IRightStatic;
of: IRightStatic; // alias for unit
pure: IRightStatic; // alias for unit
attempt<E, V>(fn: () => V): Either<Error|E, V>;
Copy link
Member

Choose a reason for hiding this comment

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

maybe there should be <E extends Error, … constraint? (also Error|E union would be obsolete, just E will do the job)

Copy link
Member

Choose a reason for hiding this comment

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

or maybe I'm wrong, as users can throw any custom stuff… but then Error is redundant in union.
Maybe attempt<E = Error, V>(fn: () => V): Either<E, V>; default value will make it more useful!

return Either.attempt(() => {throw e; });
}

function catchErrorObj<E>(): Either<Error | E, string> {
Copy link
Member

Choose a reason for hiding this comment

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

I see only Either<Error | X, … examples here. But it's also important to test how TS handles to infer these type params, and how does it fit for simple types beeing thrown..

@ulfryk
Copy link
Member

ulfryk commented Oct 26, 2018

Additionally - I was thinking about adding Try monad (with a little magic power like the one that Promise has about catching internal exceptions), but for sure explicit .flatMap(v => Either.attempt(throwable(v))) is a much better solution!

@ulfryk
Copy link
Member

ulfryk commented Jun 4, 2019

implemented in #211

@ulfryk ulfryk closed this Jun 4, 2019
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.

2 participants