Alias std.variant.Algebraic
Algebraic
data type restricted to a closed set of possible
types. It's an alias for a VariantN
with an
appropriately-constructed maximum size.
is
useful when it is desirable to restrict what a discriminated type
could hold to the end of defining simpler and more efficient
manipulation.
Algebraic
Declaration
alias Algebraic(T...) = VariantN!(maxSize!T,T)
;
Example
auto v = Algebraic!(int, double, string)(5); assert(v.peek!(int)); v = 3.14; assert(v.peek!(double)); // auto x = v.peek!(long); // won't compile, type long not allowed // v = '1'; // won't compile, type char not allowed
Example
Self-Referential Types
A useful and popular use of algebraic data structures is for defining self-referential data structures, i.e. structures that embed references to values of their own type within.
This is achieved with
by using Algebraic
This
as a placeholder whenever a
reference to the type being defined is needed. The
instantiation
will perform alpha renaming on its constituent types, replacing Algebraic
This
with the self-referenced type. The structure of the type involving This
may
be arbitrarily complex.
// A tree is either a leaf or a branch of two other trees alias Tree(Leaf) = Algebraic!(Leaf, Tuple!(This*, This*)); Tree!int tree = tuple(new Tree!int(42), new Tree!int(43)); Tree!int* right = tree.get!1[1]; assert(*right == 43); // An object is a double, a string, or a hash of objects alias Obj = Algebraic!(double, string, This[string]); Obj obj = "hello"; assert(obj.get!1 == "hello"); obj = 42.0; assert(obj.get!0 == 42); obj = ["customer": Obj("John"), "paid": Obj(23.95)]; assert(obj.get!2["customer"] == "John");