TaggedAlgebraic

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

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.

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Postblit

A postblit is present on this object, but not explicitly documented in the source.

Members

Aliases

Kind
alias Kind = TypeEnum!U

A type enum that identifies the type of value currently stored.

Type
alias Type = Kind

Compatibility alias

Union
alias Union = U

Alias of the type used for defining the possible storage types/kinds.

Functions

opBinary
auto opBinary(auto ref T other)

Enables the use of binary operators with the stored value.

opBinaryRight
auto opBinaryRight(auto ref T other)

Enables the use of binary operators with the stored value.

opCall
auto opCall(auto ref ARGS args)

Enables call syntax operations on the stored value.

opCast
T opCast()
T opCast()

Enables conversion or extraction of the stored value.

opCmp
auto opCmp(auto ref T other)

Enables relational comparisons with the stored value.

opDispatch
auto opDispatch(auto ref ARGS args)

Enables the invocation of methods of the stored value.

opEquals
auto opEquals(auto ref T other)

Enables equality comparison with the stored value.

opIndex
auto opIndex(auto ref ARGS args)

Enables indexing operations on the stored value.

opIndexAssign
auto opIndexAssign(auto ref ARGS args)

Enables index assignments on the stored value.

opOpAssign
auto opOpAssign(auto ref T other)

Enables operator assignments on the stored value.

opUnary
auto opUnary()

Enables the use of unary operators with the stored value.

toString
string toString()

Uses cast(string)/to!string to return a string representation of the enclosed value.

Properties

kind
Kind kind [@property getter]

The type ID of the currently stored value.

opDispatch
ARGS opDispatch [@property setter]

Enables accessing properties/fields of the stored value.

Examples

1 import taggedalgebraic;
2 
3 struct Foo {
4 	string name;
5 	void bar() {}
6 }
7 
8 union Base {
9 	int i;
10 	string str;
11 	Foo foo;
12 }
13 
14 alias Tagged = TaggedAlgebraic!Base;
15 
16 // Instantiate
17 Tagged taggedInt = 5;
18 Tagged taggedString = "Hello";
19 Tagged taggedFoo = Foo();
20 Tagged taggedAny = taggedInt;
21 taggedAny = taggedString;
22 taggedAny = taggedFoo;
23 
24 // Check type: Tagged.Kind is an enum
25 assert(taggedInt.kind == Tagged.Kind.i);
26 assert(taggedString.kind == Tagged.Kind.str);
27 assert(taggedFoo.kind == Tagged.Kind.foo);
28 assert(taggedAny.kind == Tagged.Kind.foo);
29 
30 // In most cases, can simply use as-is
31 auto num = 4 + taggedInt;
32 auto msg = taggedString ~ " World!";
33 taggedFoo.bar();
34 if (taggedAny.kind == Tagged.Kind.foo) // Make sure to check type first!
35 	taggedAny.bar();
36 //taggedString.bar(); // AssertError: Not a Foo!
37 
38 // Convert back by casting
39 auto i   = cast(int)    taggedInt;
40 auto str = cast(string) taggedString;
41 auto foo = cast(Foo)    taggedFoo;
42 if (taggedAny.kind == Tagged.Kind.foo) // Make sure to check type first!
43 	auto foo2 = cast(Foo) taggedAny;
44 //cast(Foo) taggedString; // AssertError!
45 
46 // Kind is an enum, so final switch is supported:
47 final switch (taggedAny.kind) {
48 	case Tagged.Kind.i:
49 		// It's "int i"
50 		break;
51 
52 	case Tagged.Kind.str:
53 		// It's "string str"
54 		break;
55 
56 	case Tagged.Kind.foo:
57 		// It's "Foo foo"
58 		break;
59 }

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

1 static struct S {
2 	int v;
3 	int test() { return v / 2; }
4 }
5 
6 static union Test {
7 	typeof(null) null_;
8 	int integer;
9 	string text;
10 	string[string] dictionary;
11 	S custom;
12 }
13 
14 alias TA = TaggedAlgebraic!Test;
15 
16 TA ta;
17 assert(ta.kind == TA.Kind.null_);
18 
19 ta = 12;
20 assert(ta.kind == TA.Kind.integer);
21 assert(ta == 12);
22 assert(cast(int)ta == 12);
23 assert(cast(long)ta == 12);
24 assert(cast(short)ta == 12);
25 
26 ta += 12;
27 assert(ta == 24);
28 assert(ta - 10 == 14);
29 
30 ta = ["foo" : "bar"];
31 assert(ta.kind == TA.Kind.dictionary);
32 assert(ta["foo"] == "bar");
33 
34 ta["foo"] = "baz";
35 assert(ta["foo"] == "baz");
36 
37 ta = S(8);
38 assert(ta.test() == 4);

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

1 static union Test {
2 	typeof(null) null_;
3 	int count;
4 	int difference;
5 }
6 
7 alias TA = TaggedAlgebraic!Test;
8 
9 TA ta = TA(12, TA.Kind.count);
10 assert(ta.kind == TA.Kind.count);
11 assert(ta == 12);
12 
13 ta = null;
14 assert(ta.kind == TA.Kind.null_);

Meta