A destructor is present on this object, but not explicitly documented in the source.
A postblit is present on this object, but not explicitly documented in the source.
A type enum that identifies the type of value currently stored.
Compatibility alias
Alias of the type used for defining the possible storage types/kinds.
Enables the use of binary operators with the stored value.
Enables the use of binary operators with the stored value.
Enables call syntax operations on the stored value.
Enables conversion or extraction of the stored value.
Enables relational comparisons with the stored value.
Enables the invocation of methods of the stored value.
Enables equality comparison with the stored value.
Enables indexing operations on the stored value.
Enables index assignments on the stored value.
Enables operator assignments on the stored value.
Enables the use of unary operators with the stored value.
Uses cast(string)/to!string to return a string representation of the enclosed value.
The type ID of the currently stored value.
Enables accessing properties/fields of the stored value.
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_);
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: