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
, aTaggedAlgebraic
is returned. - If there are multiple return values and none of them is a
Variant
, anAlgebraic
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.
Constructors
Name | Description |
---|---|
this
(other)
|
Fields
Name | Type | Description |
---|---|---|
m_union
|
TaggedUnion!U |
Properties
Name | Type | Description |
---|---|---|
kind [get]
|
TaggedAlgebraic | The type ID of the currently stored value. |
Methods
Name | Description |
---|---|
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. |
toHash
()
|
|
toString
()
|
Uses cast(string) /to!string to return a string representation of the enclosed value.
|
Aliases
Name | Description |
---|---|
FieldDefinitionType
|
|
Kind
|
A type enum that identifies the type of value currently stored. |
Type
|
Compatibility alias |
typeID
|
|
Union
|
Alias of the type used for defining the possible storage types/kinds. |
UnionType
|
The underlying tagged union type |
Templates
Name | Description |
---|---|
opDispatch
|
Enables the access to methods and propeties/fields of the stored value. |
Example
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 == Tagged .Kind .foo);
assert(taggedAny .kind == Tagged .Kind .foo);
// In most cases, can simply use as-is
auto num = 4 + taggedInt;
auto msg = taggedString ~ " World!";
taggedFoo .bar();
if (taggedAny .kind == Tagged .Kind .foo) // Make sure to check type first!
taggedAny .bar();
//taggedString.bar(); // 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 == Tagged .Kind .foo) // 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"
break;
case Tagged .Kind .str:
// It's "string str"
break;
case Tagged .Kind .foo:
// It's "Foo foo"
break;
}
Example
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);
Example
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_);