vibe.d beta banner
get vibe.d

Asynchronous I/O that doesn’t get in your way, written in D

Struct TaggedAlgebraic

Implements a generic algebraic type using an enum to identify the stored type.

struct TaggedAlgebraic(U)
if (is(U == union) || is(U == struct) || is(U == enum));

This struct takes a union or struct declaration as an input and builds an algebraic data type from its fields, using an automatically generated Kind enumeration to identify which field of the union is currently used. Multiple fields with the same value are supported.

All operators and methods are transparently forwarded to the contained value. The caller has to make sure that the contained value supports the requested operation. Failure to do so will result in an assertion failure.

The return value of forwarded operations is determined as follows:

  • If the type can be uniquely determined, it is used as the return value
  • If there are multiple possible return values and all of them match the unique types defined in the TaggedAlgebraic, a TaggedAlgebraic is returned.
  • If there are multiple return values and none of them is a Variant, an Algebraic of the set of possible return types is returned.
  • If any of the possible operations returns a Variant, this is used as the return value.


this (other)


m_union TaggedUnion!U


kind[get] TaggedAlgebraic.KindThe type ID of the currently stored value.


opAssign (other)
opBinary (other) Enables the use of binary operators with the stored value.
opBinaryRight (other) Enables the use of binary operators with the stored value.
opCall (args) Enables call syntax operations on the stored value.
opCast () Enables conversion or extraction of the stored value.
opCmp (other) Enables relational comparisons with the stored value.
opEquals (other) Enables equality comparison with the stored value.
opIndex (args) Enables indexing operations on the stored value.
opIndexAssign (args) Enables index assignments on the stored value.
opOpAssign (other) Enables operator assignments on the stored value.
opUnary () Enables the use of unary operators with the stored value.
toString () Uses cast(string)/to!string to return a string representation of the enclosed value.


Kind A type enum that identifies the type of value currently stored.
Type Compatibility alias
Union Alias of the type used for defining the possible storage types/kinds.
UnionType The underlying tagged union type


opDispatch Enables the access to methods and propeties/fields of the stored value.


import taggedalgebraic.taggedalgebraic;

struct Foo {
	string name;
	void bar() @safe {}

union Base {
	int i;
	string str;
	Foo foo;

alias Tagged = TaggedAlgebraic!Base;

// Instantiate
Tagged taggedInt = 5;
Tagged taggedString = "Hello";
Tagged taggedFoo = Foo();
Tagged taggedAny = taggedInt;
taggedAny = taggedString;
taggedAny = taggedFoo;

// Check type: Tagged.Kind is an enum
assert(taggedInt.kind == Tagged.Kind.i);
assert(taggedString.kind == Tagged.Kind.str);
assert(taggedFoo.kind ==;
assert(taggedAny.kind ==;

// In most cases, can simply use as-is
auto num = 4 + taggedInt;
auto msg = taggedString ~ " World!";;
if (taggedAny.kind == // Make sure to check type first!;
//; // AssertError: Not a Foo!

// Convert back by casting
auto i   = cast(int)    taggedInt;
auto str = cast(string) taggedString;
auto foo = cast(Foo)    taggedFoo;
if (taggedAny.kind == // Make sure to check type first!
	auto foo2 = cast(Foo) taggedAny;
//cast(Foo) taggedString; // AssertError!

// Kind is an enum, so final switch is supported:
final switch (taggedAny.kind) {
	case Tagged.Kind.i:
		// It's "int i"

	case Tagged.Kind.str:
		// It's "string str"

		// It's "Foo foo"


Operators and methods of the contained type can be used transparently.

static struct S {
	int v;
	int test() { return v / 2; }

static union Test {
	typeof(null) null_;
	int integer;
	string text;
	string[string] dictionary;
	S custom;

alias TA = TaggedAlgebraic!Test;

TA ta;
assert(ta.kind == TA.Kind.null_);

ta = 12;
assert(ta.kind == TA.Kind.integer);
assert(ta == 12);
assert(cast(int)ta == 12);
assert(cast(long)ta == 12);
assert(cast(short)ta == 12);

ta += 12;
assert(ta == 24);
assert(ta - 10 == 14);

ta = ["foo" : "bar"];
assert(ta.kind == TA.Kind.dictionary);
assert(ta["foo"] == "bar");

ta["foo"] = "baz";
assert(ta["foo"] == "baz");

ta = S(8);
assert(ta.test() == 4);


Multiple fields are allowed to have the same type, in which case the type ID enum is used to disambiguate.

static union Test {
	typeof(null) null_;
	int count;
	int difference;

alias TA = TaggedAlgebraic!Test;

TA ta = TA(12, TA.Kind.count);
assert(ta.kind == TA.Kind.count);
assert(ta == 12);

ta = null;
assert(ta.kind == TA.Kind.null_);

Sönke Ludwig


Copyright 2015-2019, Sönke Ludwig.

License, Boost License 1.0.