Type-Safe Unions in C++ and Rust
genbattle.bitbucket.org
Type-Safe Unions in C++ and Rust
1–10 of 140 posts
Re: Type-Safe Unions in C++ and Rust
#2Re: Type-Safe Unions in C++ and Rust
#3It is a feature of ALGOL68, Pascal, Ada and quite some newer languages:
Re: Type-Safe Unions in C++ and Rust
#4Re: Type-Safe Unions in C++ and Rust
#5Someone just re-invented discriminated variants from Pascal.
Apparently the majority of CS degrees don't teach history of programming languages.
On my language design lectures in the mid-90's we had to learn all major ones, all the way back to Fortran.
Re: Type-Safe Unions in C++ and Rust
#6> but it’s the first language I’ve experimented with that has made them a first-class feature It is a feature of ALGOL68, Pascal, Ada and quite some newer languages: https://en.wikipedia.org/wiki/Tagged_union
Rust has just managed to take all these features and put them together well, and strive to be more than a research language by working on things that would make others actually use the language.
(Of course, this particular feature is common in many, many languages)
Re: Type-Safe Unions in C++ and Rust
#7Someone just re-invented discriminated variants from Pascal.
This seems to have become quite prevalent. Apparently the majority of CS degrees don't teach history of programming languages. On my language design lectures in the mid-90's we had to learn all major ones, all the way back to Fortran.
Tagged unions are prevalent in so many languages that the designers of both C++17 and Rust are bound to know about them. This is "borrowing" (or "stealing" :p).
The original Rust was ML-like and had an OCaml compiler, so Rust's enums definitely descended from those. I can't talk for C++ for sure, but like I said it's common in so many languages that they're bound to have derived inspiration from them.
Re: Type-Safe Unions in C++ and Rust
#8> but it’s the first language I’ve experimented with that has made them a first-class feature It is a feature of ALGOL68, Pascal, Ada and quite some newer languages: https://en.wikipedia.org/wiki/Tagged_union
It is however a fine way to encourage sensible use of unions. But it's mostly just a suggestion.
Re: Type-Safe Unions in C++ and Rust
#9boost::variant however lacks a nice visit method that takes lambdas, instead requiring the user to create visitor classes. This verbosity may be part of the reason it wasn't adopted in mass, in spite of its advantages in terms of type safety.