-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Proposal: Anonymous discriminated unions #728
Comments
And I apologize in advance for not using correct terminology. I've built one compiler in my life and it only dealt with integers. :) |
So, you're basically asking for anonymous discriminated unions? Also, why should such type be available only as a method return type, and not also everywhere else where a normal type can appear? |
Yes, exactly! They could certainly be available more generally. The method return type was the scenario that seemed most useful when I wrote this up. |
I can easily imagine F# doing this if it did not (over-)use type inference. BTW it also sounds a bit like http://en.wikipedia.org/wiki/Postcondition |
What you've described is very very close to F#'s active patterns. I rewrote your example in F#, tell me if it's close to what you wanted in readability, then maybe the feature can be ported over!
|
I (like several others) have made a library for doing this, called OneOf which achieves this using implicit operators e.g.
I would love this to be a language feature though, as OneOf has some limitations (e.g. implicit operators don't work on interface types) |
Issue moved to dotnet/csharplang #468 via ZenHub |
Scenario: I'm currently factoring out the entity manipulation code from a web application into a standalone library for reuse and easier maintenance. A lot of it takes the following form (totally mangling the details):
To factor out the entity creation along with the validation requires that the CreateFoo method have a way to map multiple return types into the appropriate HttpStatusCodeResult or JsonResult. Anonymous discriminated unions would be the ideal solution for this, since I get the convenience of having everything defined as part of the method.
The text was updated successfully, but these errors were encountered: