> 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…
Type-Safe Unions in C++ and Rust
11–20 of 140 posts
Re: Type-Safe Unions in C++ and Rust
#12Note 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…
Re: Type-Safe Unions in C++ and Rust
#13Re: Type-Safe Unions in C++ and Rust
#14Someone just re-invented discriminated variants from Pascal.
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
#15Earlier 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.
Re: Type-Safe Unions in C++ and Rust
#16Newbie here. What are the differences with Swift Enumerations?
Re: Type-Safe Unions in C++ and Rust
#17Newbie here. What are the differences with Swift Enumerations?
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> 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…
Re: Type-Safe Unions in C++ and Rust
#19> 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
Of course I implemented them in Virgil, too. (shameless plug: https://github.com/titzer/virgil)