match

Calls a type-appropriate function with the value held in a SumType.

For each possible type the SumType can hold, the given handlers are checked, in order, to see whether they accept a single argument of that type. The first one that does is chosen as the match for that type. (Note that the first match may not always be the most exact match. See "Avoiding unintentional matches" for one common pitfall.)

Every type must have a matching handler, and every handler must match at least one type. This is enforced at compile time.

Handlers may be functions, delegates, or objects with opCall overloads. If a function with more than one overload is given as a handler, all of the overloads are considered as potential matches.

Templated handlers are also accepted, and will match any type for which they can be implicitly instantiated. See "Introspection-based matching" for an example of templated handler usage.

If multiple SumTypes are passed to match, their values are passed to the handlers as separate arguments, and matching is done for each possible combination of argument types. See "Multiple dispatch" for an example.

template match(handlers...)
match
(
SumTypes...
)
(
auto ref SumTypes args
)
if (
allSatisfy!(isSumType, SumTypes) &&
args.length > 0
)

Members

Functions

match
auto match(SumTypes args)

The actual match function.

Return Value

The value returned from the handler that matches the currently-held type.

Examples

Avoiding unintentional matches

Sometimes, implicit conversions may cause a handler to match more types than intended. The example below shows two solutions to this problem.

1 alias Number = SumType!(double, int);
2 
3 Number x;
4 
5 // Problem: because int implicitly converts to double, the double
6 // handler is used for both types, and the int handler never matches.
7 assert(!__traits(compiles,
8     x.match!(
9         (double d) => "got double",
10         (int n) => "got int"
11     )
12 ));
13 
14 // Solution 1: put the handler for the "more specialized" type (in this
15 // case, int) before the handler for the type it converts to.
16 assert(__traits(compiles,
17     x.match!(
18         (int n) => "got int",
19         (double d) => "got double"
20     )
21 ));
22 
23 // Solution 2: use a template that only accepts the exact type it's
24 // supposed to match, instead of any type that implicitly converts to it.
25 alias exactly(T, alias fun) = function (arg) {
26     static assert(is(typeof(arg) == T));
27     return fun(arg);
28 };
29 
30 // Now, even if we put the double handler first, it will only be used for
31 // doubles, not ints.
32 assert(__traits(compiles,
33     x.match!(
34         exactly!(double, d => "got double"),
35         exactly!(int, n => "got int")
36     )
37 ));

Multiple dispatch

Pattern matching can be performed on multiple SumTypes at once by passing handlers with multiple arguments. This usually leads to more concise code than using nested calls to match, as show below.

1 struct Point2D { double x, y; }
2 struct Point3D { double x, y, z; }
3 
4 alias Point = SumType!(Point2D, Point3D);
5 
6 version(none) {
7     // This function works, but the code is ugly and repetitive.
8     // It uses three separate calls to match!
9     @safe pure nothrow @nogc
10     bool sameDimensions(Point p1, Point p2)
11     {
12         return p1.match!(
13             (Point2D _) => p2.match!(
14                 (Point2D _) => true,
15                 _ => false
16             ),
17             (Point3D _) => p2.match!(
18                 (Point3D _) => true,
19                 _ => false
20             )
21         );
22     }
23 }
24 
25 // This version is much nicer.
26 @safe pure nothrow @nogc
27 bool sameDimensions(Point p1, Point p2)
28 {
29     alias doMatch = match!(
30         (Point2D _1, Point2D _2) => true,
31         (Point3D _1, Point3D _2) => true,
32         (_1, _2) => false
33     );
34 
35     return doMatch(p1, p2);
36 }
37 
38 Point a = Point2D(1, 2);
39 Point b = Point2D(3, 4);
40 Point c = Point3D(5, 6, 7);
41 Point d = Point3D(8, 9, 0);
42 
43 assert( sameDimensions(a, b));
44 assert( sameDimensions(c, d));
45 assert(!sameDimensions(a, c));
46 assert(!sameDimensions(d, b));

See Also

std.variant.visit

Meta