Live data from Hacker News

Type-Safe Unions in C++ and Rust

genbattle.bitbucket.org

11–20 of 140 posts

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

#11
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…

Ada and Pascal are not really research languages, though. But still, there are improvements taken from research, see Ada 2012 contracts for example.

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

#12

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…

They are implemented completely differently. The only real similarity is in the name and API. Boost variant suffers from significant performance penalties that the new std::variant does not have.

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

#14
post #2

Someone just re-invented discriminated variants from Pascal.

As someone pointed out elsewhere, Pascal's discriminated variants ("variant records") aren't typesafe, since the tag isn't checked before the variant is accessed. From Essential Pascal, 4th edition: "The use of a variant record type is not type-safe." The whole point of the article's discriminated unions/records in Rust and C++17 is type safety.

Maybe this has been built into one of the newer Pascal descendants, but it hasn't been around for years and years. And embedded C programmers have been using these kinds of "unsafe" discriminated unions for years.

That said, I do think Ada has had these kinds of actually typesafe variant records for quite a while. But again, Rust isn't making any claims to innovation. Even the language name ("Rust"), is a reference to the language being based on rusty old best practices.

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

#15
post #11

Earlier quoted context omitted.

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…

Ada and Pascal are not really research languages, though. But still, there are improvements taken from research, see Ada 2012 contracts for example.

Right, like I said, this particular feature exists in a million languages. I'm talking about the whole set of features that Rust has; some are from research languages and may have never been seen in the industry before (e.g. regions/borrow checking), but they're not really new. This particular feature in Rust descended from the ML family (since Rust used to be ML-like), which in turn probably got it from Ada or w/e.

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

#17

Newbie here. What are the differences with Swift Enumerations?

Compared to Rust enumerations, they are one and the same. In fact, looking at the documentation for Swift Enumerations yields this little nugget:

You can define Swift enumerations to store associated values of any given type, and the value types can be different for each case of the enumeration if needed. Enumerations similar to these are known as discriminated unions, tagged unions, or variants in other programming languages. [1]

Haskell and other ML family languages also have similar constructs, although these are usually modelled using "Algebraic Data Types", or "sum types". [2] [3]

[1]: https://developer.apple.com/library/content/documentation/Sw...

[2]: https://wiki.haskell.org/Algebraic_data_type

[3]: https://www.schoolofhaskell.com/school/to-infinity-and-beyon...

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

#18
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…

Yes, worth noting that this was an initial goal of Rust: to use research that was rarely implemented, but established and non-novel among CS academics. See http://tim.dreamwidth.org/1784423.html -- I remember some comment about this being part of the choice of the name, too, but I can't find anything specific or definitive about that.

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

#19
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

They look a lot nicer in ML and Haskell, IMO.

Of course I implemented them in Virgil, too. (shameless plug: https://github.com/titzer/virgil)

Post reply on HN