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

Boost License 1.0

Authors

Paul Backus

Source: std/sumtype.d

Types 3

structThis

Placeholder used to refer to the enclosing [SumType].

structSumType(Types...) if (is(NoDuplicates!Types == Types) && Types.length > 0)

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

"Arithmetic expression evaluator" 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

Fields
bool canHoldTag
Storage storage
Tag tag
Methods
inout(T) get(T)() if (IndexOf!(T, Types) >= 0) @trusted ref inout
bool opEquals(this This, Rhs)(auto ref Rhs rhs) if (!is(CommonType!(This, Rhs) == void))Compares two `SumType`s for equality.
private structTagTuple(SumTypes...)
Fields
size_t[SumTypes.length] tags
Methods
TagTuple fromCaseId(size_t caseId)
size_t toCaseId()
Constructors
this(ref const(SumTypes) args)

Functions 2

private fnsize_t stride(size_t dim, lengths...)()
private fnvoid destroyIfOwner(T)(ref T value)

Variables 2

private enumvarisSumTypeInstance = is(T == SumType!Args, Args...)

True if T is an instance of the SumType template, otherwise false.

enumvarisSumType = is(T : SumType!Args, Args...)

True if T is a [SumType] or implicitly converts to one, otherwise false.

Templates 5

tmplDeducedParameterType(T)
tmplmatch(handlers...)

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 [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

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

See Also

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

The actual match function.

Parameters

argsOne or more [SumType] objects.
tmplcanMatch(alias handler, Ts...) 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.

tmplIota(size_t n)
tmplmatchImpl(Flag!"exhaustive" exhaustive, handlers...)
Functions
auto ref matchImpl(SumTypes...)(auto ref SumTypes args) if (allSatisfy!(isSumType, SumTypes) && args.length > 0)