Live data from Hacker News

Type-Safe Unions in C++ and Rust

genbattle.bitbucket.org

31–40 of 140 posts

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

#31

Earlier quoted context omitted.

well, yes. The nice thing is that in C++ can be implemented purely as a library. Sometimes C++ feels like the high level languages assembler.

> The nice thing is that in C++ can be implemented purely as a library. The bad thing is that they suck.

They are certainly nowhere as nice as a builtin language feature, but they are not too bad. They are in fact quite usable.

In the next 10 years C++ might even grow another leg and incorporate them in the basic language (together with proper pattern matching).

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

#32

Earlier quoted context omitted.

> The nice thing is that in C++ can be implemented purely as a library. The bad thing is that they suck.

They are certainly nowhere as nice as a builtin language feature, but they are not too bad. They are in fact quite usable. In the next 10 years C++ might even grow another leg and incorporate them in the basic language (together with proper pattern matching).

In languages where they are builtin they get used quite a lot to great effect, whereas this is cumbersome in C++, both with boost and the new std::variant stuff. "usable" might not cut it for something you want to use ubiquitously :)

It's good that C++ has this, but it hinders some programming patterns. Of course, C++ has other programming patterns that it's great at to compensate :)

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

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

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 "truly unique" is way off. Even parrot has a better, safe and lockless threading model, which guarantees safety.

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

#34
post #33

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…

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 safety when required.

edit: accidentally a word

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

#35
post #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 )

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?

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

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

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.

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

#37
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're specifically saying that it's the first language they've experimented with , not the first language in general, FFS.

I know, right. The full quote is apparently less fun to argue against:

> I’m aware the idea for type-safe unions isn’t unique or original to Rust, but it’s the first language I’ve experimented with that has made them a first-class feature.

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

#38
post #20

Isn't this just an algebraic data structure?

well, yes. The nice thing is that in C++ can be implemented purely as a library. Sometimes C++ feels like the high level languages assembler.

Unfortunately that means they lack lots of the pattern matching niceties that you get in languages with builtin ADTs. There is an impressive paper about implementing pattern matching on sub-classes, but it's pretty hackily done using the preprocessor, and could definitely do with some language support: http://www.stroustrup.com/OpenPatternMatching.pdf

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

#39
post #33

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…

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…

> You still have to manually maintain locks, and it's also much slower.

You ... don't. That's just the concurrency model that the abstractions in the stdlib expose, but Rust's safety is generic enough that you can use different abstractions (e.g. lockfree ones). See crossbeam for some alternative models. There are also transactional memeory impls in Rust. They still use Send and Sync for safety though. Manually maintaining locks is a feature of the concurrency library used, not the safety model.

Pony's system is actually pretty close to that of Rust. Sync is "immutable", Send is "isolated" (sort of). Of course, capabilities are different from auto traits, but the idea behind using these two capabilities for concurrency safety is similar.

(So I was wrong that Rust's concurrency safety system is unique, since Pony has something based on the same concepts)

> There's a safety model, but it's only best practice, not enforced by the language nor the compiler.

Yeah, Send and Sync are technically a part of the stdlib (and can be reimplemented outside of it, aside from a small interaction with statics -- Send/Sync are treated as special by the compiler when it comes to statics). However, it is enforced by the compiler in the sense that if you avoid best practice (using Send and Sync), you can't write parallel code without dropping to `unsafe`. If you do that you can design safe abstractions around that and use whatever concurrency enforcement you wish, though pretty much everyone sticks to Send and Sync since it works well with the rest of the language.

(Because of the statics thing, "not part of the language" is debatable, anyway)

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

#40
post #36

Earlier quoted context omitted.

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…

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.
Post reply on HN