C++Now 2019 has ended
Back To Schedule
Monday, May 6 • 11:00 - 12:30
Matchine: Pattern Matching for Open Sum Types

Log in to save this to your schedule, view media, leave feedback and see who's attending!

Feedback form is now closed.
C++17 offers std::variant to model closed sum types and do pattern matching on them using std::visit (and eventually std::overloaded). While this is not as powerful as pattern matching in languages having this built in, it still allows to have a decent declarative design using mere library features.

It is also common to have this problem over an underlying open sum type, which can be modeled by means of inheritance and dynamic_cast or std::any, thus, relying on RTTI. This approach has two flaws compared to std::variant: firstly, dispatching using RTTI is much slower than std::variant and, secondly, C++ lacks a declarative counterpart for open sum types.

In this talk I will present solutions to both problems. I will show how a declarative dispatcher that works similarly to std::visit and the proposed std::overloaded can be implemented over open sum types. Further I will present an implementation that allows to model open sum types by inheritance but without relying on RTTI. By using both tools together a fast declarative pattern matching over type hierarchies can be built. The presented tools have been open sourced to 'matchine', a small library, and are already running in production code.

In the (more crazy) final part I will explore further possibilities of pattern matching as library feature. In particular I want to show some experiments of sorting the matching cases at compile time in order to do a binary search at run (match) time.

avatar for André Bergner

André Bergner

Software Engineer, Apple
André likes challenges – that's why he enjoys writing C++ and tries to do things with it that it wasn't supposed to do. He works as an principal software engineer at Native Instruments in Berlin. He holds a PhD in theoretical physics and besides C++ is toying a lot with deep learning... Read More →

Monday May 6, 2019 11:00 - 12:30
Flug Auditorium

Attendees (43)