std.sumtype
SumType is a generic discriminated union implementation that uses design-by-introspection to generate safe and efficient code. Its features include:
- Pattern matching.
- Support for self-referential types.
- Full attribute correctness (
pure,@safe,@nogc, andnothroware inferred whenever possible). - A type-safe and memory-safe API compatible with DIP 1000 (
scope). - No dependency on runtime type information (
TypeInfo). - Compatibility with BetterC.
List of examples
- Basic usage
- Matching with an overload set
- Recursive SumTypes
- Memory corruption (why assignment can be
@system) - Avoiding unintentional matches
- Multiple dispatch
License
Boost License 1.0
Types 3
A tagged union that can hold a single value from any of a specified set of types.
The value in a SumType can be operated on using pattern matching.
To avoid ambiguity, duplicate types are not allowed (but see the "basic usage" example for a workaround).
The special type This can be used as a placeholder to create self-referential types, just like with Algebraic. See the "Recursive SumTypes" example for usage.
A SumType is initialized by default to hold the .init value of its first member type, just like a regular union. The version identifier SumTypeNoDefaultCtor can be used to disable this behavior.
See Also
bool canHoldTagStorage storage@trusted ref getByIndex(size_t tid)() if (tid < Types.length) inoutbool opEquals(this This, Rhs)(auto ref Rhs rhs) if (!is(CommonType!(This, Rhs) == void))Compares two `SumType`s for equality.size_t[typeCounts.length] tagsthis(ref const SumTypes args)Functions 2
Variables 2
isSumTypeInstance = is(T == SumType!Args, Args...)True if T is an instance of the SumType template, otherwise false.
isSumType = is(T : SumType!Args, Args...)True if T is a SumType or implicitly converts to one, otherwise false.
Templates 10
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 spec/expression 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.
Returns
See Also
The actual match function.
Parameters
args | One or more SumType objects. |
if (Ts.length > 0)True if handler is a potential match for Ts, otherwise false.
See the documentation for match for a full explanation of how matches are chosen.
Checks whether a SumType contains a value of a given type.
The types must match exactly, without implicit conversions.
Parameters
T | the type to check for. |
Accesses a SumType's value.
The value must be of the specified type. Use has to check.
Parameters
T | the type of the value being accessed. |
The actual get function.
Parameters
self | the SumType whose value is being accessed. |
Returns
SumType's value.