5 ms·
I don’t know about the others, but Rust absolutely has tagged unions. It’s just the enum type. edit: just checked the others. D has https://dlang.org/phobos/st
by skavi 4y ago
I don’t know about the others, but Rust absolutely has tagged unions. It’s just the enum type.
edit: just checked the others. D has https://dlang.org/phobos/std_sumtype.html https://dlang.org/phobos/std_sumtype.html and C++ has std::variant. both seem a bit clunky, but usable.
- Kukumber 4y agoLast i checked, Rust tagged union was "unsafe", what's unsafe has no place in Rust [1] About D, a template is not Tagged Union, it's a template in a module from the standard library, it's not a language feature, it makes me less interested about that language knowing that fact, they follow the same path as C++, ``std::`` crap [1] - https://doc.rust-lang.org/reference/items/unions.html https://doc.rust-lang.org/reference/items/unions.html
- Rusky 4y agoThat's a gross misunderstanding of `unsafe`'s place in Rust, and also of the use cases for `union` vs `enum`.
- Kukumber 4y agoI'm not well versed in Rust, care to educate me to clear misunderstanding? What's the Rust translation of this example?: https://ziglang.org/documentation/master/#Tagged-union https://ziglang.org/documentation/master/#Tagged-union
- TakeBlaster16 4y agoenum MyEnum { Ok(u8), NotOk } fn main() { let value = MyEnum::Ok(42); assert!(matches!(value, MyEnum::Ok(_))); match value { MyEnum::Ok(x) => assert_eq!(x, 42), MyEnum::NotOk => unreachable!(), } } No unsafe to be found. The feature is there, it just has a different keyword than you might be used to -- the same way Haskell and Java both have something called "class" that mean different things.
- Cyph0n 4y agoRust enums are a form of tagged unions. Rust unions are plain old unions that are mainly used for C interop. This is why using them requires unsafe code.
- Ar-Curunir 4y agoIn Rust plain C-style `union`s (used with the `union`) keyword are associated with `unsafe`. The ergonomic, standard replacement is `enum`. So you would do, e.g., `enum Option<T> { Some(T), None }` to represent a nullable value in the type system.
- WalterBright 4y agoA strength of a language's features is if the user can extend it with library types. Needing special syntactic and semantic sugar is a sign of expressive weakness. (Though too much expressability can also lead to problems, like adding nitro injection to your car.)
- Kukumber 4y agoThat's true, but in the case of Tagged Union it improves safety, readability and better support for tooling, templates introduce slower compilation and noisy build error, and now you depend on the runtime, it advertise template capabilities of the language for sure, but at what cost.. Why bother with slices when you can have a template? struct Slice(T) { T* ptr; size_t len; } ;) Another example, this time with Zig, they refuse to have interface/trait, now everyone has to duplicate bogus code, even them in their std, sure it promotes the language capabilities, but at the cost of poor ergonomics, that'll prevent their growth
- WalterBright 4y agoIndeed. D used to have complex numbers built in, but the library type turned out to be more practical.
- skavi 4y agoThe D std sumtype is actually super impressive after taking a longer look at it. Crazy how nice it looks to use for something that's not a language construct.
- germandiago 4y agoD is the king of practical metaprogramming for me. I think it stands out for compile-time metaprogramming beyond the competition. Many ppl talk Lisp and Nim and such. They are even more powerful. But I honestly get lost with those and not with D. Idk why, maybe my familiarity with C++ helps.
- 4y ago
- chlorion 4y agoYou seem to be confused which is understandable. By tagged unions people mean the "enum" data type. Rust enums are a little different than in other languages that you are likely used to. They can act like the classic C enums that you are probably used to, where they are more or less just named constants grouped together under a single type. Then, unlike the classic C style enum, they can also hold data in them, where each variant is basically a struct with named fields and everything. They also support methods just like structs do (the methods are per enum and not per variant). In cases where you don't care about the fields names, you can define the variant as a tuple and later access fields by index. Struct/Tuple enums are actually implemented as tagged unions by the compiler but this is completely invisible to you so you don't really need to care about it! Enums are also totally "safe" to create and use! Rust also has a union type which is like a C union, and you could use it to implement tagged unions (requires unsafe) if you wanted to, but in almost all cases you will just use "enum" for this purpose. Overloading the "enum" into two different things seems like a strange choice to me but I don't know what the history is behind it, and it takes just a few minutes to figure it out so it's not a huge deal!