Repository navigation
[Proposal]: Discriminated unions #10400
Unanswered
CyrusNajmabadi
asked this question in
Language Ideas
Replies: 2 comments 16 replies
|
I think I much prefer this to the expanded enum design. |
0 replies
Why isn't it possible to introduce some default implementation for |
16 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Discriminated unions
An alternate approach builds discriminated unions on C# enums.
Summary
This proposal builds on the shipped
unionfeature. A normal union is a type union: its alternatives can overlap, sounion Content(string, object)cannot distinguish a string supplied asstringfrom the same string supplied asobject. Declared cases preserve that distinction, even when their payloads have the same or overlapping types.Each
casedeclares a nested record and adds it to the union. Building on the existing union model lets the design reuse union behavior and keeps type unions and discriminated unions closely aligned.Sample walkthrough:
Parse("42")uses target-typed generic type inference, which is not specific to unions. The.Some(...)and.Noneforms depend on the contextual case syntax extensions.Supporting Documents
The directory overview maps the supporting documents:
enum structthat avoids boxing during construction and extraction.This is the community discussion for the proposal; LDM design work is tracked in the champion issue.
All reactions