3 ms·
> One such cleanup: we’ve made a strong start on banishing unions and type punning from the code. These are not going to translate into any language with the co
by awinter-py 10y ago
> One such cleanup: we’ve made a strong start on banishing unions and type punning from the code. These are not going to translate into any language with the correctness properties we want.
Really? This sounds like idiomatic rust to me (heavy with enums).
- Ericson2314 10y agoC unions have no discriminant. But yeah it's a pity—I'd try to hack up discriminated unions then. Or convert to Rust unions and then enums and remove unsafety.
- awinter-py 10y agoC union declarations should correspond pretty closely to rust enum declarations. It's a surprisingly important feature. Every large codebase I've worked on has clunky workarounds for storing heterogeneous types in collections. Haven't seen a silver bullet; dynamic languages are great at this until you have to scale (either in lines of code, number of types or dataset size) and then they become unmanageable. Static languages force you to type a lot and build in-memory ETL-style transformations but scale better. Using SQL is another solution, but that separates you from the language's type-checker and can be another source of error. Type safety at the serialization boundary was the promise of CORBA and protobuf but we need better tools. I hope we see more focus on ser/des types in the next generation of industry languages.
- steveklabnik 10y agoClose, but not quite: unless the type is NonZero, you need space for the tag. c-style unions are in nightly behind a flag; they're not stable yet.
- deleted 10y ago[deleted]
- Manishearth 10y agoI think they meant that the use case corresponds closely, not the representation. Generally C unions get used the same place Rust unions would in a rust program (not vice versa though), unless they're being used for type punning, which is pretty rare anyway.
- awinter-py 10y agoI meant if you have a large program where many types are C unions like: struct TaggedUnion { enum {T1, T2} type_id; union {type1 t1; type2 t2;} u; } If the goal is to translate automatically to a rust enum declaration, that will be (a) possible to do with an automatic tool and (b) will give enhanced safety because it will force the type to be checked everywhere these values are used.
- yxhuvud 10y agoAt least that one is really simple in Crystal: x = [] of Int32 | Char x << 42 x << 'F' => [42, 'F']