3 ms·
> One core requirement for this is what Rust calls an Enum and what is broadly known as a Union type Union types and enum types are not the same thing, and thi
by adastra22 2y ago
> One core requirement for this is what Rust calls an Enum and what is broadly known as a Union type
Union types and enum types are not the same thing, and this misunderstanding invalidates the entire article. An enum type includes a marker which indicates which value it contains. The garbage collector would be able to read this tag value and know.
- hmry 2y agoI don't know what to tell you, there are just multiple ways to use the same words. What Rust calls enums are called tagged unions in most other languages. Enum usually refers to a simple collection of named values, what Rust calls fieldless enums or unit-only enums.
- Tainnor 2y ago> are called tagged unions in most other languages Not sure about that. Swift also calls them enums, in Java and Kotlin (and maybe Scala? I forget) they're sealed classes/interfaces and in PL theory and many typed FP languages they're called "sum types".
- pjmlp 2y agoVariant records in Pascal, and related linage language. Enumerations are a sequence of values.
- adastra22 2y agoThe "tagged" part is important. They are in fact not called "unions" in most languages, only C-derived languages. And in C, unions are untagged. Only later extensions added tagged union types. So I would contend that unqualified "union type" means an untagged union.
- cookiengineer 2y agoWouldn't an enum in go not only be different in what the reflections API sees behind the scenes? In the sense that it first points to the type, and then to the value, similar to how other newer types are already implemented? type whatever enum { value1 iota value2 } enum would just be a similar shorthand to uint or whatever you want to identify it uniquely. I don't see much problem implementing this apart from the methods in the GC that have to be touched to trace the object tree correctly. The comparison handling must be touched in either case, so I don't think an additional pointer to the enum's definition there counts as much work.
- IshKebab 2y agoIt sounds like you don't have exposure to other languages. C defaults to untagged unions but there are plenty of languages where a union is always tagged. To put it another way, a Rust `enum` is not the same as C's untagged unions but it is still a union.
- adastra22 2y agoThe article says the garbage collector can't know which variant the union type is. I don't know how to interpret that other than an (incorrect) assumption of untagged unions.
- IshKebab 2y agoIt is written a little confusingly but that's not what they're saying. They were saying you can't implement a union in user code with the current garbage collector because there's no way to tell it which variant your union is: > let's ask why we can't implement such a union type today using Go's unsafe package to perform suitable manipulation of a suitable memory region. They are aware that you could add union types to Go - their point is that it would require garbage collector modifications which may be difficult: > The corollary to all of this is that adding union types to Go as a language feature wouldn't be merely a modest change in the compiler. It would also require a bunch of work in how such types interact with garbage collection, Go's memory allocation systems (which in the normal Go toolchain allocate things with pointers into separate memory arenas than things without them), and likely other places in the runtime.
- aiono 2y agoMeaning of union and enum changes based on which programming language terminology you use. Enum of Rust is tagged union of C with compiler support. Enum in C is just basic enum of Rust without any fields. The list goes on like this.