match

Synopsis

template <class Pattern, class Expression>
struct match
{
  // unspecified
};

Description

Metafunction implementing pattern matching. It matches the angly-bracket expression Expression against the pattern Pattern, which has to be a syntax.

Every class matches itself. The following two cases are exceptions:

When the expression matches the pattern, match returns a boost::mpl::map, where they keys are the open variables of the pattern and the values are the classes that were matched against them. The values of the map are syntaxes.

When the expression doesn't match, match returns a bad_match exception.

#include <mpllibs/metamonad/match.hpp>

Expression semantics

For any x and y classes, t1 and t2 templates taking two classes as arguments the following are equivalent:

using namespace boost::mpl;

match<syntax<int>, int>::type
map<>

match<syntax<_>, int>::type
map<>

match<syntax<int>, double>::type
exception<...>

match<syntax<var<x>>, int>::type
map<pair<var<x>, syntax<int>>>

match<syntax<t1<var<x>, var<y>>>, t1<int, double>>::type
map<pair<var<x>, int>, pair<var<y>, syntax<double>>>

match<syntax<t1<var<x>, var<y>>>, t2<int, double>>::type
exception<...>

match<syntax<t1<var<x>, var<x>>>, t1<int, int>>::type
map<pair<var<x>, syntax<int>>>

match<syntax<t1<var<x>, var<x>>>, t1<int, double>>::type
exception<...>

match<syntax<var<x>>, t1<int, double>>::type
map<pair<var<x#, t1<int, double>>>

Example

using namespace mpllibs::metamonad::name;

template <class A>
struct maybe_something :
  boost::mpl::if_<
    typename boost::is_same<A, int>::type,
    just<double>,
    nothing
  >
{};

typedef
  boost::mpl::at<
    match<syntax<just<var<x>>>, maybe_something<int>::type>::type,
    var<x>
  >::type
  this_is_double;

[up]