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

and_then doesn't re-wrap #53

Open
jariji opened this issue May 31, 2023 · 0 comments
Open

and_then doesn't re-wrap #53

jariji opened this issue May 31, 2023 · 0 comments

Comments

@jariji
Copy link

jariji commented May 31, 2023

julia> Try.and_then(Ok(1)) do x
           Ok(x + 1)
       end
Try.Ok: 2

This example requires the user to wrap the value in Ok manually. An alternative API would automatically re-wrap the value in Ok, making it easier to retain the disjoint sum-type Ok{T} | Err{E} style that I often want.

Undisciplined use of and_then allows creating T | Err{E}

julia> Try.and_then(Ok(1)) do x
           x + 1
       end
2

which might be better avoided.

Is this and_then worth the risk of misuse? There are other functions that could be provided, like these in Rust.

The only situation I can think of where this would be needed is in an exotic sum type like

True | False | Unknown

where there is more than one success branch. But I'd usually represent this concept with the isomorphic

Ok(true) | Ok(false) | Err(Unknown)

which doesn't require the re-wrapping. And even with the multi-success type, users could always do manual pattern matching themselves.

Am I wrong about this? If not, what do you think about either removing and_then or documenting a requirement that the function argument return a wrapper type?

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

1 participant