-
Notifications
You must be signed in to change notification settings - Fork 167
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
Nested calls to multiple methods returning OneOf<T1, T2, ...> #150
Comments
I personally combine It's not perfect since you still need to manually specify that you want to convert an
|
By making the generic type of Match explicit, I got this to work:
So even though the return type of |
I've been playing with OneOf and it would be nice to have a command to expand a OneOf for cases like this. Like OneOf<T1, T2, T3,... TNplus1> OneOf<T1, T2, T3,... TN>.WithType<TNplus1>() {
return new OneOf<T1, T2, T3,... TNplus1>(/* some kind of magic that gets the values from OneOf<T1, T2, T3,... TN> */);
} Then it's easy to passthrough an existing OneOf or bolt on additional possibilities as needed. Obviously it's quite brittle since you wouldn't be free to fully rearrange the OneOf. That way madness lies. But the trivial case of wrapping a method and adding one or more possible output types becomes easy as you do |
I'm really enjoying the concept of discriminated unions, but I'm thinking perhaps my understanding of OneOf's implementation is somewhat lacking. In my codebase, I have implemented OneOf in several packages that are common between projects. Some consuming classes have dependencies on more than one of these common packages, and each one returns relevant types in each their public methods. In multiple places in my codebase now, I'm having to implement conditional checking using
IsTn
because it's no longer semantically possible to useMatch
orSwitch
. I've prepared a (very much simplified) example:Here we can see that the return values from the three methods are unique. Even if it made sense to do so,
return getUserResult
won't compile becauseOneOf<NotFound, B2CUser>
does not matchOneOf<NotFound, Error, Expired, Success>
. But because I need to return early, it means I have to useTryPickn
orIsTn
, as shown in this example.In my real codebase, due to the nature of the dependencies and their possible return types, this has led to an explosion of conditional returns that visually pollute the codebase and make it less readable.
So, my question is... Is there something I am missing, some concept or feature that would simplify my multitude of conditions? Or is this just a price of using discriminated unions in a statically-typed language?
The text was updated successfully, but these errors were encountered: