2 ms·
> At one level we easily do something that looks like a Result type in Go, especially now that we have generics. You make a generic struct that has private fiel
by eru 2y ago
> At one level we easily do something that looks like a Result type in Go, especially now that we have generics. You make a generic struct that has private fields for an error, a value of type T, and a flag that says which is valid, and then give it some methods to set and get values and ask it which it currently contains. If you ask for a sort of value that's not valid, it panics. However, this struct necessarily has space for three fields, where the Rust enums (and generally union types) act more like C unions, only needing space for the largest type possible in them and sometimes a marker of what type is in the union right now.
That seems better than not having algebraic data types at all.