View source code
Display the source code in std/sumtype.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Struct std.sumtype.SumType

A [tagged union](https://en.wikipedia.org/wiki/Tagged_union) that can hold a single value from any of a specified set of types.

struct SumType(Types...)
  
if (is(NoDuplicates!Types == Types) && (Types.length > 0));

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](#basic-usage) 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](#arithmetic-expression-evaluator) 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

NameDescription
this (value) Constructs a SumType holding a specific value.
this (other) Constructs a SumType that's a copy of another SumType.

Methods

NameDescription
opAssign (rhs) Assigns a value to a SumType.
opAssign (rhs) Copies the value from another SumType into this one.
opAssign (rhs) Moves the value from another SumType into this one.
opEquals (rhs) Compares two SumTypes for equality.
toHash () Returns the hash of the SumType's current value.
toString () Returns a string representation of the SumType's current value.
toString (sink, fmt) Handles formatted writing of the SumType's current value.

Aliases

NameDescription
Types The types a SumType can hold.

See Also

Algebraic

Authors

Paul Backus

License

Boost License 1.0