Live data from Hacker News

Type-Safe Unions in C++ and Rust

genbattle.bitbucket.org

1–10 of 140 posts

Re: Type-Safe Unions in C++ and Rust

#5
post #2

Someone 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.

Re: Type-Safe Unions in C++ and Rust

#6
post #3

> 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

One interesting thing about Rust is that none of the language features are really new. Even the borrow checker is from research papers and languages from quite a while ago. The only thing I can think of that might be truly unique to Rust is the concurrency safety model (Send+Sync), though that might be old too.

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

#7
post #5
post #2

Someone 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.

This really doesn't seem like a reinventing to me (from either the Rust or C++ side). "Reinventing" implies ignoring history in the way you mention.

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
post #3

> 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

The tag in Pascal is (a) optionally stored (only the type of the tag is needed to distinguish between the different variants in the declaration, the field can be omitted) and (b) not actually strongly typed (that is, the tag is not consulted before access to one of the variants is made).

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

#9
Note C++2017's std::variant is based upon boost::variant, which has been around since at least 2004 (http://www.boost.org/doc/libs/1_31_0/doc/html/variant.html, http://www.boost.org/users/history/).

boost::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.

Post reply on HN