Mach7: Pattern Matching for C++

Here is a new library to perform pattern matching:

Mach7: Pattern Matching for C++

by Yuriy Solodkyy, Gabriel Dos Reis and Bjarne Stroustrup

From the article:

Pattern matching is an abstraction mechanism that can greatly simplify source code. Commonly, pattern matching is built into a language to provide better syntax, faster code, correctness guarantees and improved diagnostics. Mach7 is a library solution to pattern matching in C++ that maintains many of these features. All the patterns in Mach7 are user-definable, can be stored in variables, passed among functions, and allow the use of open class hierarchies...

Example:

// Fibonacci numbers
int fib(int n)
{
    var<int> m;

    Match(n)
    {
      Case(1)     return 1;
      Case(2)     return 1;
      Case(2*m)   return sqr(fib(m+1)) - sqr(fib(m-1));
      Case(2*m+1) return sqr(fib(m+1)) + sqr(fib(m));
    }
    EndMatch
}

Add a Comment

Comments are closed.

Comments (1)

0 0

grizzlysmit said on Oct 10, 2017 10:04 PM:

I like this Idea a way we can extend the types switch copes with, without messing with switch and potentially breaking code, it would be nice to have something other than break to end a case (breakcase ??), so break can still break the containing loop, also a synonym for case would be good same case but takes only one statement and doesn't need a break or what ever (breakcase??)