dyaml.stdsumtype
This module was copied from Phobos at commit 87c6e7e35 (2022-07-06). This is necessary to include https://github.com/dlang/phobos/pull/8501 which is a fix needed for DIP1000 compatibility. A couple minor changes where also required to deal with package(std) imports.
[SumType] is a generic discriminated union implementation that uses design-by-introspection to generate safe and efficient code. Its features include:
[Pattern matching.][match] Support for self-referential types. Full attribute correctness (pure, @safe, @nogc, and nothrow are 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.
License
Types 3
Placeholder used to refer to the enclosing [SumType].
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][match].
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
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 storageTag tagbool opEquals(this This, Rhs)(auto ref Rhs rhs) if (!is(CommonType!(This, Rhs) == void))Compares two `SumType`s for equality.size_t[SumTypes.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 5
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 anexample of templated handler usage.
If multiple [SumType]s 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.