Pre-RFC: unsafe enums. Now including a poll!

if a #[repr(union)] struct has two "fields" with the same type but different names, they still refer to the same thing -- it fundamentally doesn't make sense to have names

I can certainly imagine cases where the type provides sufficient information, but in many of the interfaces I've worked with, names for fields make the code more clear. I'd rather identify a field of a union as "bytes_remaining" than as "u64". And if a second field "records_remaining" exists that happens to also have type "u64", I'd rather use the different names associated with different semantic meanings, even if the types happen to coincide.

That said, just as both structs and tuple structs exist, perhaps it might make sense to also support a kind of union with unnamed fields accessed by type. But I don't think that should be the only form of union available.

2 Likes