-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add proposal for enhanced enums in C# #9713
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?
Conversation
Updated enum syntax to include 'class' or 'struct' keywords and clarified rules for shape enums.
|
||
Each case with a parameter list generates a nested record type. `enum class` generates `sealed record class` types; `enum struct` generates `readonly record struct` types. While these are the generated types, the union implementation may optimize internal storage. | ||
|
||
TODO: Should the structs be readonly? Seems like that goes against normal structs (including `record struct`). Seems like that could be opt in with `readonly enum struct X`. |
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.
This is an interesting question. I want to note that giving out refs to the fields which implement the case payloads can be problematic, especially mutable refs.
If an enum struct member, wants to be mutable, e.g. reassign the whole this
to implement StateMachine.Transition(input)
, mutating the receiver variable, that is probably ok.
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.
Yup. This def seems like an ldm question. Inline with tuples and record-structs, i'm leanign toward this being mutable. Basically, we could just say that you get records, and the behavior that comes with them. Then if you really awnt readonly, you opt in, just like with record sstructs.
Co-authored-by: Rikki Gibson <[email protected]>
enum class Result<T> | ||
{ | ||
Success(T value), | ||
Error(string message); |
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 find this mix of commas and semicolons exceptionally unwieldy in Java enums.
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.
One thing i didn't fully understand; is that an alternative proposal to unions, or in addition to?
Logically they feel very similar - and thus is an alternative - but the way it's worded at times ("Shape enums inherit all union pattern matching behavior:") seems to imply unions already exist in the spec
|
||
Each case with a parameter list generates a nested record type. `enum class` generates `sealed record class` types; `enum struct` generates `readonly record struct` types. While these are the generated types, the union implementation may optimize internal storage. | ||
|
||
TODO: Should the structs be readonly? Seems like that goes against normal structs (including `record struct`). Seems like that could be opt in with `readonly enum struct X`. |
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.
readonly enum ref struct
?
In addition to. As the spec states, these can just compile down to unions. |
Co-authored-by: Rikki Gibson <[email protected]>
No description provided.