3 ms·
The C++ (or Java etc) way to do many of the kind of things people do with Rust pattern matching & sum types would be via OO subtyping polymorphism. e.g. classic
by cmrdporcupine 4y ago
The C++ (or Java etc) way to do many of the kind of things people do with Rust pattern matching & sum types would be via OO subtyping polymorphism. e.g. classic visitor pattern, etc. Way more verbose and awkward, and scatters the logic all over the place.
Enums + switch is the other, and far less powerful.
- thom 4y agoOr, for the case of errors, with exceptions, which happen to have their own very limited pattern matching syntax.
- kaba0 4y agoI swear I am not paid to shill for Java or anything…, but Java did get sum types recently, and it’s quite good. They made it in a backwards compatible manner by sealed classes that list all of its subtypes. Switch expressions were also implemented, and they can exhaustively match on the given sum type. Pattern matching is quite limited as of now (only usable with records), but it is coming.
- cmrdporcupine 4y agoI should take a look. I kind of have a nostalgic interest in looking back at the JVM again, as 10 years ago it was my main career. The problem with Java was always that it has moved so slowly in acquiring nice things. The actual core runtime is amazingly well engineered.