-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expanded enums and unions #9712
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
base: main
Are you sure you want to change the base?
Changes from all commits
cfedfe4
adf1d86
77de8ff
b8256f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,131 @@ | ||||||
| # Enums and unions | ||||||
|
|
||||||
| ## Abstract | ||||||
|
|
||||||
| This proposal separates two high-level concepts into two separate syntaxes: `enum` and `union`. Unions are a type declaration that represent the union of existing types. Enums are a compositional type syntax. They allow declaring a sequence of nested types, each of which are unioned into the parent type. This is also known as an "algebraic data type". Union declarations take a list of existing types as input, while the enhanced enum syntax takes a list of type names, parameter lists, and bodies. | ||||||
|
|
||||||
| ## Example | ||||||
|
|
||||||
| Union declaration: | ||||||
|
|
||||||
| ```C# | ||||||
| union StringOrInt | ||||||
| { | ||||||
| string, | ||||||
| int | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| Enum declaration: | ||||||
|
|
||||||
| ```C# | ||||||
| enum JsonValue | ||||||
| { | ||||||
| Number(double value), | ||||||
| Bool(bool Value), | ||||||
| String(string Value), | ||||||
| Object(ImmutableDictionary<string, JsonValue> Members), | ||||||
| Array(ImmutableArray<JsonValue> Elements), | ||||||
| Null | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| ## Syntax | ||||||
|
|
||||||
| Expanded enum: | ||||||
| ```bnf | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my pref is we stick with g4. IT's how we do all our grammars :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot rewrite to g4 syntax |
||||||
| <type-declaration> ::= "enum" <type-name> "{" <variant-list> "}" | ||||||
| <variant-list> ::= <variant> { "," <variant> } [ "," ] | ||||||
| <variant> ::= <variant-name> [ "(" <field-list> ")" ] | ||||||
| <field-list> ::= <field> { "," <field> } | ||||||
| <field> ::= <type> <field-name> | ||||||
| <type> ::= <simple-type> | <generic-type> | ||||||
| <simple-type> ::= "double" | "bool" | "string" | <type-name> | ||||||
|
||||||
| <simple-type> ::= "double" | "bool" | "string" | <type-name> | |
| <simple-type> ::= "double" | "bool" | "string" | "int" | <type-name> |
Copilot
AI
Oct 6, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The semicolon after 'Null' is inconsistent with the proposed enum syntax shown earlier in the document where variants are separated by commas. This example should use consistent syntax.
| Null; | |
| Null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i like the practical example.