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

Thoughts on an orElse/unwrapOrElse equivalent? #23

Open
abrenneke opened this issue Mar 29, 2023 · 1 comment
Open

Thoughts on an orElse/unwrapOrElse equivalent? #23

abrenneke opened this issue Mar 29, 2023 · 1 comment

Comments

@abrenneke
Copy link

I'm liking the lib so far! What are your thoughts on orElse methods? Right now if I want to have errors fallback I do:

const res = fn().chain(
  (x) => Result.ok(x),
  () => otherFn()
);

But that could be made a little more ergonomic if it was something like:

const res = fn().orElse((err) => otherFn());

Or the equivalent unwrap:

const val = fn().unwrapOrElse((err) => otherFn());

I get that it's a balance of keeping the lib small and having things be ergonomic - rust has soo many Result methods 😆

@sam-mfb
Copy link

sam-mfb commented Jul 19, 2024

how would unwrapOrElse work? what would be the type of val in an error scenario?

is the idea that unwrapOrElse would always throw in the error case? if not, how do you handle the fact that val continues to live on in outer scope in an error situation?

i like the idea of these ergonomics but i don't see anyway to not return control to the outer scope unless you are always throwing after otherFn() is called.

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