-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Labels
Milestone
Description
Nominal Type Unions
- Specification: Nominal Type Unions
- Working group folder: Discriminated Unions
- Discussion: [Discussion] Nominal Type Unions #9410
Summary
Named unions have a closed list of "case types":
public union IntOrString(int, string);
Fresh case types can be succinctly declared if we adopt the "Case Declarations" proposal:
public union Pet
{
case Cat(...);
case Dog(...);
}
Case types convert implicitly to the union type:
Pet pet = dog;
Patterns apply to the contents of a union. Switches that handle all case types are exhaustive:
var name = pet switch
{
Cat cat => ...,
Dog dog => ...,
// No warning about missing cases
}
Related Proposals
- https://github.com/dotnet/csharplang/blob/main/meetings/working-groups/discriminated-unions/TypeUnions.md
- https://github.com/dotnet/csharplang/blob/main/meetings/working-groups/discriminated-unions/original-nominal-type-unions.md