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 Variant, match and tap #37

Closed
JohannesKlauss opened this issue Mar 23, 2023 · 4 comments
Closed

Add Variant, match and tap #37

JohannesKlauss opened this issue Mar 23, 2023 · 4 comments

Comments

@JohannesKlauss
Copy link

Thank you for the updated library, are you interested in adding the Variant Implementation to enable matching, like proposed in this PR vultix#61 ? That would be a tremendous improvement

@jstasiak
Copy link
Collaborator

jstasiak commented Apr 3, 2023

Hey, that's a good question. Do we have details about this new API, what's the motivation behind it, what problems does it solve etc.? Difficult to say without knowing that.

@JohannesKlauss
Copy link
Author

It would allow you to get rid of if else branching in your code, making it a more readable control flow like the matching pattern in scala, kotlin, rust, etc.

So instead of something like this:

const result = createSomeResult()

if(result.ok) {
  // Do something with result
}

// result is an error

You can do something like this:

match(createSomeResult(), {
  Ok: val => // Happy path,
  Err: val => // Error Path,
})

You can also do that with Some and None. This would provide a simple way to use pattern matching on those implementations

@jstasiak
Copy link
Collaborator

jstasiak commented Apr 4, 2023

Thanks for the response. There's already a bunch of methods on Result and Option to (help) achieve exactly that, are these insufficient? If so, do you have an example or two?

@JohannesKlauss
Copy link
Author

Yes you're right. map And mapErr are providing basically the same functionality.

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