Hacker News new | ask | show | jobs
by jokoon 1401 days ago
I hope this will become part of C++ one day.

Although if the C++ committee manages to add this without deprecating other things, I would be impressed.

1 comments

There has been a proposal to add pattern matching to C++ for nearly a decade now https://www.stroustrup.com/OpenPatternMatching.pdf Shame it never got in. With language support for matching, sum types and product types it would be a completely different language.
The regex library in the STL was quite a lesson for the C++ maintainers. They will probably not allow anything similar any time soon.
Although the phrase "pattern matching" is involved these are completely different features.

Also, unlike for regular expressions what you actually want here is typically a language feature, even if a bunch of the lifting is done in your standard library. In particular you probably want a keyword (or in several C++ pattern matching proposals, more than one) and new behaviour not just a few functions and constants.

You could think of this as a bit like "switch" but on the other hand if your insight into switch is that it's basically a computed go-to wearing fancy dress then no, not that, the thing ordinary people use switch for.

Wouldn't it be very similar to Rust? (or OCaml - but without the automatic memory management)
Actually this library (match(it)) is more similar to Racket. You can see there are lots of patterns borrowed from Racket pattern matching, say app pattern and ooo pattern.