-
Notifications
You must be signed in to change notification settings - Fork 5
Closed
Description
This is a proposal to add associated values to Slice enum, similar to Swift and Rust.
See:
https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html
https://doc.rust-lang.org/rust-by-example/custom_types/enum.html
The syntax of Slice enums with associated values is similar to Rust/Swift:
enum WebEvent
{
PageLoad,
PageUnload,
KeyPress(byte key),
Paste(string str),
Click(long x, long y)
}
Syntactically, it's similar to an operation with parameters and no return value.
A Slice enum where any enumerator has one or more associated values:
- cannot be unchecked
- cannot have an explicit value for any of its enumerators (like = 5)
- cannot have an underlying integer type (like
enum WebEvent : byte
)
In C#, WebEvent and its enumerators would map to a small generated record hierarchy:
public record WebEvent
{
public sealed record PageLoad() : WebEvent;
public sealed record PageUnload() : WebEvent;
public sealed record KeyPress(byte Key) : WebEvent;
public sealed record Paste(string Str) : WebEvent;
public sealed record Click(long X, long Y) : WebEvent;
}
Usage: https://dotnetfiddle.net/LH5C6q
This also works well with switch case and switch expression, since we can match on the type (like WebEvent.KeyPress).
bentoi, InsertCreativityHere and pepone