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. (Remember that a function literal without an explicit argument type is considered a template.)

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 value types. See "Multiple dispatch" for an example.

template match(handlers...)
ref
match
(
SumTypes...
)
(
auto ref SumTypes args
)
if (
allSatisfy!(isSumType, SumTypes)
)

Members

Functions

match
auto ref match(SumTypes args)
Undocumented in source. Be warned that the author may not have intended to support it.

Return Value

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

Meta