Live data from Hacker News

Type-Safe Unions in C++ and Rust

genbattle.bitbucket.org

61–70 of 140 posts

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

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

Yup, a lot of people like to say X has Y, however execution of Y is just as important(if not moreso) than the initial idea of Y. Technical ideas don't thrive in isolation, they need to be nurtured and grown.

Just look at the dominance of Javascript in the programming landscape, you're seeing decades of high quality executions of the language(I include community in the "execution" definition) despite it being far from the best technical language.

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

#62

Earlier quoted context omitted.

Lot of syntactic sugar recently added to C++ and a lot more in C++17. The structured bindings in particular are really excellent ways for unpacking tuples and structs with type inference into new variables automatically.

Yeah but if you read the article and look at the syntax for C++ match-like statements, it leaves a lot to be desired.

For pattern matching, that's true. And my response is, stay tuned. C++ has a lot more to offer in this area in the future. std::variant is opening a big door for all kinds of new features in the language, and for those who cannot wait, there are pattern matching libraries out there to peak at.

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

#63
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.

sorry if this is a dumb question, but what classifies a language as a research language?

That's a great question!

I would probably restate it though. What is research vs. a product. I just read about 1nm transistors, but we're probably looking at 10 years before Intel, et al, have built all the infrastructure to reliably deliver a CPU based on it.

In the case of a language, I would say it's similar, do you have the support infrastructure in place? Rust is amazing in this regard: Cargo, crates.io, docs.rs, rustup, etc. on top of that you have at least one large company and many others pushing the language in a large and distributed product.

I would classify Rust as a production ready coding platform.

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

#64

Earlier quoted context omitted.

Yeah but if you read the article and look at the syntax for C++ match-like statements, it leaves a lot to be desired.

For pattern matching, that's true. And my response is, stay tuned. C++ has a lot more to offer in this area in the future. std::variant is opening a big door for all kinds of new features in the language, and for those who cannot wait, there are pattern matching libraries out there to peak at.

I feel like this point is often made about C++: "in the future we will have that feature too!"

If you don't want to wait, want to experiment with something else, learn a new language, etc., Rust is awesome.

Obviously the counter argument to this is, "but there's 30+ years of C++ in production", but be honest, who actually wants to work on a 30 year old codebase?

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

#65

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…

the concurrency safety model (Send+Sync), though that might be old too Smalltalk has had that since the early 1980s

I'd like to read more about this, do you have a recommendation of where I can look? I have a basic fluency in Smalltalk, but it's been a while.

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

#66
post #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.…

It depends, do you want a piece of code that will be more reliable and require less maintenance after development?

Even after more than a year of learning the language I do find that some things take me longer to build, but the end product is far better.

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

#67
post #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.…

It depends, do you want a piece of code that will be more reliable and require less maintenance after development? Even after more than a year of learning the language I do find that some things take me longer to build, but the end product is far better.

I can write reliable code requiring less maintenance in C or C++. What you're engaging in is language zealotry. Rust isn't a panacea.

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

#68

Earlier quoted context omitted.

For pattern matching, that's true. And my response is, stay tuned. C++ has a lot more to offer in this area in the future. std::variant is opening a big door for all kinds of new features in the language, and for those who cannot wait, there are pattern matching libraries out there to peak at.

I feel like this point is often made about C++: "in the future we will have that feature too!" If you don't want to wait, want to experiment with something else, learn a new language, etc., Rust is awesome. Obviously the counter argument to this is, "but there's 30+ years of C++ in production", but be honest, who actually wants to work on a 30 year old codebase?

GCC is very close to the 30-year mark (0.9 released 22 March 1987). Linux turned 25 this past year; Firefox (the C++ parts, at least) is very close to 25, depending on what code you want to count. Apache is 20ish years old. Even LLVM, which I think of as a relatively young project, is getting close to 15 years. Maybe (hopefully?) very little code survives from the initial versions, but they've all withstood the test of time.

It's certainly pleasing to be able to sit down and write something from scratch or nearly from scratch. But it can be equally pleasing to extend a multi-million line codebase to do something new, or to enable the codebase as a whole to do something new/better.

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

#69
post #67

Earlier quoted context omitted.

It depends, do you want a piece of code that will be more reliable and require less maintenance after development? Even after more than a year of learning the language I do find that some things take me longer to build, but the end product is far better.

I can write reliable code requiring less maintenance in C or C++. What you're engaging in is language zealotry. Rust isn't a panacea.

I think you're reading into the comment too much. Rust by definition, will create a safer variant of whatever similar code you write in C or C++. This isn't really debatable, things like bounds checking on arrays, strongly typed error results, thread safe memory sharing semantics. These will absolutely guarantee that in general you will have safer code in Rust.

What I said is that it might take you longer to write it in Rust, than something similar in C/C++, but you won't have some of the guarantees you get from the Rust semantics. So the tradeoff is up to you; write code faster, or write code safer.

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

#70

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…

the concurrency safety model (Send+Sync), though that might be old too Smalltalk has had that since the early 1980s

that's interesting, AFAIK smaltalk is fully dynamic and has no compile-time type checking, so how would it statically enforce the equivalent of Send and Sync constraints?
Post reply on HN