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.

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.

Constructors

this
this(T value)
this(const(T) value)
this(immutable(T) value)
this(Value value)

Constructs a SumType holding a specific value.

this
this(inout(SumType) other)
this(SumType other)
this(const(SumType) other)
this(immutable(SumType) other)

Constructs a SumType that's a copy of another SumType.

Destructor

~this
~this()

Calls the destructor of the SumType's current value.

Members

Aliases

Types
alias Types = AliasSeq!(ReplaceTypeUnless!(isSumTypeInstance, This, typeof(this), TemplateArgsOf!SumType))

The types a SumType can hold.

Functions

opAssign
SumType opAssign(T rhs)

Assigns a value to a SumType.

opAssign
SumType opAssign(SumType rhs)

Copies the value from another SumType into this one.

opAssign
SumType opAssign(SumType rhs)

Moves the value from another SumType into this one.

opEquals
bool opEquals(Rhs rhs)

Compares two SumTypes for equality.

toHash
size_t toHash()

Returns the hash of the SumType's current value.

toString
string toString()

Returns a string representation of the SumType's current value.

toString
void toString(Sink sink, FormatSpec!Char fmt)

Handles formatted writing of the SumType's current value.

typeIndex
size_t typeIndex()

Returns the index of the type of the SumType's current value in the SumType's Types.

See Also

std.variant.Algebraic

Meta