-
Notifications
You must be signed in to change notification settings - Fork 165
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
Possibility to implement TryPick<Tx> #151
Comments
Your code sample doesn't work as provided. It produces an error ("CS0029 Cannot implicitly convert type 'T0' to 'T0'") and a warning ("CS0693 Type parameter 'T0' has the same name as the type parameter from outer type 'OneOf<T0, T1>'"). If you rename the type parameter (but not the type of the first parameter) then it would compile. But then it doesn't make any sense since you would always try to get |
I held off doing it this way when I wrote OneOf in case people ever found that the types they were using were covariant. In my old age I'm softening on this and thinking that having generic Is TryPick etc would be good, but I wouldn't want to remove the old methods, and I wouldn't want to balloon the assembly size with new methods. What to do? |
Would those methods make such a big difference? Like going from 100 kB (what we have now) to 1 MB or something like that? I have more concerns about the correctness of such a method.
|
Additionally @aliriocastro, how would |
@wdolek A pattern I've been using is something like
That way your happy path happens in a block, then error handling happens in a normal switch call. If you repeat this pattern, it also doesn't tie consumers of OneOf's to be tied to the author's generics order. That was actually one of the original reasons I was interested in the approach we took with DiscU some years ago. I think the As I mentioned in another issue though, it does introduce the possibility for subtle bugs if you use |
@jamesbascle if I understand it correctly (I'm struggling to find anything related to this in DiscU), proposal for Implement every combination of Tn (this won't scale and prevent use of
Implement generics (but I don't see how to constraint
Wrap remainder in another
Could you extend your example? When I tried to implement my own DU, I ended up with similar pattern as in OneOf, just without having |
if you have this OneOf
Then you can write some code that looks like this
When I mentioned DiscU, one of the things I was looking to get away from was the imposition on the consumer of a DU, by the author of it. Specifically that imposition is that if you want to use the In practice, I think something like the following example controller method stub is a pretty good pattern as well.
The |
Re first example, it could just be: if (x.TryPickT0(out Success s))
{
}
else if (x.TryPickT1(out Error1 e1))
{
}
// ... ... right? Just keep trying to pick value from Second example suggests Nevertheless, I would dare to say that arity above two is niche use case. For example, in my team we have introduced base |
@wdolek Like I said, It feels to me like an imposition upon the user to force them to write their logic in a particular order, in order to get the benefits of exhaustive type checking. That was the essentially goal of DiscU - allow users to write their logic in any manner they wish, but still provide the exhaustive type checking. It does that in a similar manner to the The pattern you've shown w/ the repeated if blocks doesn't do exhaustive type checking, and is functionally equivalent to just calling I've decided that I'd rather use a more bog standard library such as |
I was going to ask for the same function signature with a different output. I am after TryPick to give me the output if it is of type T, regardless of the position within the OneOf. This is extremely useful for scenarios like custom state records, where each of the types in the OneOf would be different. For instance, doing a TryPick on a OneOf<Off, Idle, Active>, would give back the Active record if it is set. To handle where a type is used more than once, you could return a new OneOf encapsulating all of them. For instance, doing a TryPick on a OnefOf<string, int, string>, would return OneOf<string, string>. Not sure if this is possible but would be extremely useful. |
Hello Team, I'm just using the library and I feel fascinated. However, I would like to propose the following and check if it is possible.
I just built the code to explain myself:
The point is, instead of forcing to T[index], we could use the type.
I hope I could explain myself.
The text was updated successfully, but these errors were encountered: