A useful and popular use of algebraic data structures is for defining $(LUCKY self-referential data structures), i.e. structures that embed references to values of their own type within. This is achieved with Variant by using This as a placeholder whenever a reference to the type being defined is needed. The Variant instantiation will perform alpha renaming on its constituent types, replacing This with the self-referenced type. The structure of the type involving This may be arbitrarily complex.
import mir.functional: Tuple = RefTuple; // A tree is either a leaf or a branch of two others alias Tree(Leaf) = Variant!(Leaf, Tuple!(This*, This*)); alias Leafs = Tuple!(Tree!int*, Tree!int*); Tree!int tree = Leafs(new Tree!int(41), new Tree!int(43)); Tree!int* right = tree.get!Leafs[1]; assert(*right == 43);
// An object is a double, a string, or a hash of objects alias Obj = Variant!(double, string, This[string], This[]); alias Map = Obj[string]; Obj obj = "hello"; assert(obj._is!string); assert(obj.trustedGet!string == "hello"); obj = 42.0; assert(obj.get!double == 42); obj = ["customer": Obj("John"), "paid": Obj(23.95)]; assert(obj.get!Map["customer"] == "John");
Gets TaggedType tag name.