by PreInternet01 · 2y ago · 0 comments
private enum Derp { }
private enum Foo { }
var derp = (Derp)12;
var foo = (Foo)99;
foo = derp;
However, this is definitely a hack, and I sort-of feel the same about the Zig solution from the article. Would be nice if languages had 'cleaner' support for this? struct Foo(u32);
No? record struct Foo(int Value);
In F#, this is instead done with // Foo and Bar cannot be assigned to each other
type Foo = Foo of int
type Bar = Bar of int
// Foo and Bar can be assigned to each other
type Foo = int
type Bar = int
The first variant uses single-case unions. It's a bit unfortunate that they also default to classes over structs unless you annotate them with [<Struct>], partially fixed by escape analysis though.The enum trick is the tersest and will also serialize correctly in most cases, but I have never seen anyone use it like that before.