Live data from Hacker News

Type-Safe Unions in C++ and Rust

genbattle.bitbucket.org

41–50 of 140 posts

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

#41
post #35
post #19

Earlier quoted context omitted.

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 )

I don't intend to derail the discussion further, but I'm curious about Virgil. I can't find any information on it that tells me who is behind it or how to contact them. Do you have anywhere I could look to learn more about the project in general?

Sure! It's pretty much just my perennial side project since at least 2004. Some information is available in publications (latest: http://dl.acm.org/citation.cfm?id=2491962), and there is some documentation in the wiki branch (https://github.com/titzer/virgil/tree/wiki).

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

#42
Came here to say that for the specific problem at hand (`ConnectionState`) you should probably use session types in Rust -- but someone on /r/rust beat me to it _and_ the author already added it to the post while I was reading! :)

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

#43
post #36

Earlier quoted context omitted.

Only if development of the language and OS are separate. If you co-develop the language with the OS, then it makes perfect sense to push safety features into the language (or conversely, to remove them from the language when they are no longer appropriate). LISP-strength macros give you most of this capability.

OS are not the only use cases for system languages and developing a language to be tightly tied to an os (and viceversa) is a great way to condemn both to obscurity.

Erm... I think Dennis Ritchie would have had a few things to say about that.

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

#45
post #43

Earlier quoted context omitted.

OS are not the only use cases for system languages and developing a language to be tightly tied to an os (and viceversa) is a great way to condemn both to obscurity.

Erm... I think Dennis Ritchie would have had a few things to say about that.

good point; though, while designed to work well with each other, neither unix nor C require the other.

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

#46
post #11

Earlier quoted context omitted.

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.

ML predates Ada by about 7 years, so that's unlikely. The way it is implemented in ML is most likely just an implementation of the mathematical concept of sum types rather than a feature influenced by existing programming languages.

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

#47
post #33

Earlier quoted context omitted.

Putting them well together in Rust is debatable. Writing proper and safe concurrent code in Rust still looks horrible compared to languages which supports it natively in the type system, e.g. ponylang type capabilities. You still have to manually maintain locks, and it's also much slower. There's a safety model, but it's only best practice, not enforced by the language nor the compiler. So calling it "safe" and "trul…

Having a language-imposed concurrency model would be useless and actively harmful in a system language. Rust provides the building blocks to build whatever safe abstractions are appropriate for the problem and domain at hand, plus a set of out of the box abstractions relatively low level that will be familiar to people coming from other system languages. And of course the means to get rid of any abstraction and safet…

> Having a language-imposed concurrency model would be useless and actively harmful in a system language.

It works fine in Ada.

Edit: Why am I downvoted for this? A language-defined concurrency model is indispensable for having safe and deadlock-free concurrency in the language (rather than in arbitrary utility libraries). That's why concurrency was explicitly included into Ada and into the additional Ravenscar profile. The rationale for this has always convinced me and it also works fine in Ada, so anybody care to elaborate what's wrong with it?

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

#48
This is neat. I'm writing a very badly constructed toy application to get used to Rust, and some of the differences have been interesting to note.

In this case, when I first encountered Rust's enums the first thing that came to mind is the fact that C and C++ both offer the ability to support tagged-unions, but certainly not as a first-class entity and definitely with a lot more cruft, with or without safety checks.

For me the jury is still out on where the best "fit" for Rust is. I really appreciate the enforced safety of Rust for higher-level systems applications programming. I'm not convinced yet it won't just get in the way pointlessly for much lower level programming (especially embedded). Perhaps that's just my relative novice understanding of Rust, though.

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

#49
post #46

Earlier quoted context omitted.

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.

ML predates Ada by about 7 years, so that's unlikely. The way it is implemented in ML is most likely just an implementation of the mathematical concept of sum types rather than a feature influenced by existing programming languages.

> ML predates Ada by about 7 years, so that's unlikely.

Ha! I didn't know that. Thanks :)

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

#50
The first example could have been implemented in C or C++ using a union. Wouldn't necessarily be type-safer, but the reminder of writing myConnection.connected.m_id instead of just myConnection.m_id is also a pretty good way to ensuring that you remember to check if (myConnection.m_connectionState == CONNECTED) before ever accessing myConnection.connected. That said, having compiler errors is much better. I'd love to be able to achieve this in pure C. One way might be to use opaque types with accessor functions to return a pointer to the correct part of the union according to the requested connection state, a bit like std::variant::get_if.
Post reply on HN