add slice Sum type #1488
Replies: 2 comments
-
Hi Eric, We call these types "enum with associated values", which is another name for sum types and discriminated unions. And yes, we want to add enum with associated values to Slice in the near future: it's a useful building block that can't be emulated with the existing constructed types. The encoding is straightforward: it would be the integral enum value followed by the associated value. The language mapping is more difficult because Ice supports many languages without direct support for "enum with associated values".
In C#, we can map an enum with associated values to a record (class) hierarchy, as in: enum WebEvent
{
PageLoad,
PageUnload,
KeyPress(byte key),
Paste(string str),
Click(long x, long y)
} 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;
} Is there an even better solution coming soon? Best regards, |
Beta Was this translation helpful? Give feedback.
-
Here is the full proposal / discussion to add enum with associated values to Slice: icerpc/slicec#30 We want to implement in Rust before we implement it in C#, since Rust supports this construct natively. |
Beta Was this translation helpful? Give feedback.
-
Sum types are increasingly being adopted by modern languages such as Typescript, Swift, Rust, Kotlin, decent support in recent Java and coming soon to C#, etc
Because they are incredibly useful for robust API modeling.
Is there a possibility of introducing Sum type support to Slice?
Beta Was this translation helpful? Give feedback.
All reactions