Hacker News new | past | comments | ask | show | jobs | submit login

Variant types where one has to use objects as types don’t come up that often in API design or data structures in my experience with mobile and system programming on e.g. Linux. I think I’ve genuinely had to use them only a few times.

Error types are probably the most popular incarnation of that. There’s several libraries available and they will be part of the C++ standard.

This is a case of the Rust community overselling a minor feature as a game-changing novelty.




> Variant types where one has to use objects as types don’t come up that often in API design or data structures in my experience with mobile and system programming on e.g. Linux

You can't use what you don't have, so you adapt to the tools you do have. In my C++ time, the team would often write types that logically held several variants. However they were expressed as product types, so with space overhead and error-prone, unergonomic use.

Sum types are game-changing. Look at any Rust project you'll find enums with data everywhere. They're just a building block to model problems, like product types are. A language that miss them is as strange too me as a language without product types. After 10 years of mostly C++11 and C++14 I would never go back to it for this reason alone (although as outlined in the article there are other reasons too).

> Error types are probably the most popular incarnation of that. There’s several libraries available and they will be part of the C++ standard.

Again, with what performance and ergonomics?


I just checked the old Rust classic “ripgrep” on github.

In matcher.rs, the enum is used as poor man’s OOP. I saw several match expressions which then call the same function on each matched type.

searcher/mod.rs is an error definition.

glob.rs contains a sort of policy enum which can be implemented once again with OOP or as a policy template.

json.rs usage can be modeled as a single class.

core/app.rs is more involved, but can be modeled as a series of structs with a map from enum -> any. Or as am std::variant. Or using OOP.

I looked at all instances and didn’t see anything game-changing. It’s a nice syntax and it should have really good performance, but such idioms are way too low-level to change any game.


This is a straw man. There is nobody on this Earth claiming that sum types are necessary to solve a problem. Product types aren't necessary either. You could just write everything in Assembly. Or maybe raw bytecode if you want.

As should be patently fucking obvious to anyone who has been commenting on a technology web site for as long as you have, sum types are a tool. They are a tool for expressing clearly and concisely the idea that a value can be exactly one of several possible options. That tool then interacts with the rest of the language based on that invariant, sometimes providing things like exhaustiveness checking and pattern matching. Put all this together, and you have a very succinct and very clear way of representing certain kinds of values in a program.

Your comment might as well go through ripgrep and talk about how functions aren't needed. "They could have just used goto here and there."

> I looked at all instances and didn’t see anything game-changing. It’s a nice syntax and it should have really good performance, but such idioms are way too low-level to change any game.

Sum types were game changing to me when I learned about them over a decade ago. Since then, they have been a significant factor in how I think about and structure data in programs.

I have zero interest in trying to convince someone like you that you should think it's game changing. That's not the point. Maybe you could do some perspective taking and realize that others might just think differently than you.


not to diminish value of sum types (those are not specific to Rust of course) but I think that product types belong to a database / config rather than being hardcoded.


I disagree. Whenever there is a null pointer in the API, you could (and probably should) be using a sum type instead.

Sum types are not novel (Standard ML had them 40 years ago), but I think they are indeed a game changer.


You’ve described std::optional, a special case of std::variant.


> This is a case of the Rust community overselling a minor feature as a game-changing novelty.

Sum types reify control flow into an object from which said control flow can be retrieved. Compiler checked sum types remove the possibility of retrieving inconsistent control flow.

The transform is equivalent to callback to future.

It's one of the things that looks unimportant until you use it. After that, the absence is repeatedly experienced when working with C++. We don't use tagged unions much because the ergonomics are terrible.


You clearly haven't tried using variant types properly then. Sure, `std::variant` is pretty unergonomic, but they really are game changing. Most of my types are variant types


Most of your types? Well that certainly deserves congratulations and I hope someone will take advantage of the opportunity.

I think you misunderstood me though if you say I haven’t used them properly. I just didn’t need them or find them that useful because it rarely happens that I want to model a type which has several states.

Specific variant types like std::optional or the Result-equivalents are useful, but not that game changing either. They’re nice I suppose.

I wonder what kind of software you write that such low-level coding idioms make a big difference to the end result. Or what do you mean by game changing?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: