Discriminated Unions Proposal #2962
Replies: 19 comments 18 replies
-
ValueType DUsAre you considering ValueType DUs to help reduce allocations. I think rust's design involving a union and a tag should work in c#, as it is possible to specify that fields in a struct should occupy the same location. Top Level MembersIf the enum is represented as an abstract class, should it be possible to add top level members to the enum? I think Niko Matsakis suggestions for DU improvements in Rust which I know @agocke has seen, do a good job of explaining the benefits of allowing this. I'll try to link them soon. |
Beta Was this translation helpful? Give feedback.
-
I linked the blog posts in an earlier comment here |
Beta Was this translation helpful? Give feedback.
-
Yes, we discussed this explicitly during LDM. The intent is that you can declare a regular partial part of enum Type and put anything you want in there. We also talked about ending the enum section with a semicolon (ie Java-style) and letting you put standard class stuff after that as well. We still need to design if/how base constructors would work in these scenarios, but it's very much on the table. |
Beta Was this translation helpful? Give feedback.
-
I'm up for value-type unions What we cannot do is to make generic struct types for patterns like: result-or-exception, T-or-T[], possibly even Value-or-Object-with-Value. |
Beta Was this translation helpful? Give feedback.
-
Really excited about this; Discriminated Unions are the one feature of any language that I sorely miss with C# and cannot really replicate using other means. Like your designs 👍 I'm puzzled as to why these will differ from other types though in being |
Beta Was this translation helpful? Give feedback.
-
The use of |
Beta Was this translation helpful? Give feedback.
-
Just added the notes from our discussion of this proposal on Wednesday: #2968 re: struct discriminated unions, I think we should do them and there's a natural parallel here, but it's just out of scope for me right now. I'm trying not to design too many things at the same time |
Beta Was this translation helpful? Give feedback.
-
@agocke Any thoughts yet on generic discriminated unions? Since C# doesn't have a bottom type (aka Nothing or Never) it's not going to be possible to have a generic DU with non-generic cases (or cases with a different arity from the base). The easy example of this would be either |
Beta Was this translation helpful? Give feedback.
-
I don't believe anywhere else the language spec talks about implementation constraints of O(N) vs O(log(N)). For small N, a Logn scaling implementation (or even hotpath optimisation based on frequency of occurrence from profiling) is often quicker than the overhead of guaranteeing a constant time implementation. eg: https://github.com/dotnet/csharplang/blob/5dd11df6976beb761c68091cda13475f88ecd9a7/proposals/csharp-8.0/patterns.md eg: how compilers implement non-enum switch statements with small (or large) numbers of branches. often "guaranteed constant time" involves some type of hash, jump and collision resolution, which would be more effort than log_2(n) nested if's. |
Beta Was this translation helpful? Give feedback.
-
I expect you would define Option as essentially enum class Option<T>
{
Some(T t),
None
} It's true that you don't have a single None instance for all Option types, but I don't think it's that much of a problem. The only drawback I can see is that when mapping from an |
Beta Was this translation helpful? Give feedback.
-
@agocke I thought as much. Will there be any special type inference support, perhaps with #2926 (Name lookup for simple name when target type known)? Basically, what I'd really like to avoid is having to write In C# |
Beta Was this translation helpful? Give feedback.
-
Right, |
Beta Was this translation helpful? Give feedback.
-
@Richiban we might get a bottom type in some later version of C#, #538 has been championed. @agocke any chance #538 could be re-triaged to the same milestone as DUs? |
Beta Was this translation helpful? Give feedback.
-
I don't see how a bottom type would help you in this scenario, since classes are invariant. |
Beta Was this translation helpful? Give feedback.
-
As I said before, I'm very much looking forward to getting these DUs! There's a lot of work to be done in the future to make them fully featured, however. For example, this is how you properly model an
Unfortunately it makes use of quite a number of features that C# doesn't have:
In addition, it uses a record (marked by the |
Beta Was this translation helpful? Give feedback.
-
I don't think this is necessary for all use cases. I doubt this will be in V1 regardless, since variance is a part of the CLR type system and this would require also changing the CLR. |
Beta Was this translation helpful? Give feedback.
-
FYI, our current expectation is yes. More precisely, we expect amortized constant time performance based on runtime support that we are planning in .Net core 5. Having said that, we cannot prevent someone else from supplying a "bad" runtime implementation that violates these expectations. |
Beta Was this translation helpful? Give feedback.
-
Linking to #3760 for future reference |
Beta Was this translation helpful? Give feedback.
-
I know there might be a number of DU proposals currently open... does anyone know if any of them is championed? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@agocke added a proposal for discriminated Unions a few hours ago. I wanted to make a few comments, but you can't comment on a proposal so I thought I'd make this issue to discuss.
The proposal is at https://github.com/dotnet/csharplang/blob/master/proposals/discriminated-unions.md
Beta Was this translation helpful? Give feedback.
All reactions