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.

Implicit conversions are not taken into account, except between differently-qualified versions of the same type. For example, a handler that accepts a long will not match the type int, but a handler that accepts a const(int)[] will match the type immutable(int)[].

Every type must have a matching handler. This is enforced at compile-time.

Handlers may be functions, delegates, or objects with opCall overloads. Templated handlers are also accepted, and will match any type for which they can be implicitly instantiated. See "Structural matching" for an example of templated handler usage.

template match(handlers...)
match
(
Self
)
(
auto ref Self self
)
if (
is(Self : SumType!TypeArgs,
TypeArgs...
)
)

Members

Functions

match
auto match(auto ref Self self)

The actual match function.

Return Value

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

See Also

std.variant.visit

Meta