Skip to content

[Proposal]: Nominal Type Unions #9411

@mattwar

Description

@mattwar

Nominal Type Unions

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

LDM Notes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions