Construct an algebraic type from its subset.
Allowed types list
get overload that accept .Algebraic.Kind.
trustedGet overload that accept .Algebraic.Kind.
Algebraic Kind.
Checks if the underlaying type is an element of a user provided type set.
Checks if the storage stores an allowed type.
Defined if the first type is typeof(null)
Gets the value if not null. If this is in the null state, and the optional parameter fallback was provided, it will be returned. Without fallback, calling get with a null state is invalid.
Gets an algebraic subset.
Defined if the first type is typeof(null)
Defined if the first type is typeof(null)
Requires mir-algorithm package
nothrow .Algebraic.get alternative that returns an algebraic subset.
Zero cost always nothrow get alternative
Gets the value if not null. If this is in the null state, and the optional parameter fallback was provided, it will be returned. Without fallback, calling get with a null state is invalid.
Defined if AllowedTypes contains void
Gets an algebraic subset.
nothrow .Algebraic.get alternative that returns an algebraic subset.
Constructor and methods propagation.
1 static struct Base 2 { 3 double d; 4 } 5 6 static class C 7 { 8 // alias this members are supported 9 Base base; 10 alias base this; 11 12 int a; 13 private string _b; 14 15 @safe pure nothrow @nogc: 16 17 string b() const @property { return _b; } 18 void b(string b) @property { _b = b; } 19 20 int retArg(int v) { return v; } 21 22 this(int a, string b) 23 { 24 this.a = a; 25 this._b = b; 26 } 27 } 28 29 static struct S 30 { 31 string b; 32 int a; 33 34 double retArg(double v) { return v; } 35 36 // alias this members are supported 37 Base base; 38 alias base this; 39 } 40 41 static void inc(ref int a) { a++; } 42 43 alias V = Nullable!(C, S); // or Variant! 44 45 auto v = V(2, "str"); 46 assert(v._is!C); 47 assert(v.a == 2); 48 assert(v.b == "str"); 49 // members are returned by reference if possible 50 inc(v.a); 51 assert(v.a == 3); 52 v.b = "s"; 53 assert(v.b == "s"); 54 // alias this members are supported 55 v.d = 10; 56 assert(v.d == 10); 57 // method call support 58 assert(v.retArg(100)._is!int); 59 assert(v.retArg(100) == 100); 60 61 v = V("S", 5); 62 assert(v._is!S); 63 assert(v.a == 5); 64 assert(v.b == "S"); 65 // members are returned by reference if possible 66 inc(v.a); 67 assert(v.a == 6); 68 v.b = "s"; 69 assert(v.b == "s"); 70 // alias this members are supported 71 v.d = 15; 72 assert(v.d == 15); 73 // method call support 74 assert(v.retArg(300)._is!double); 75 assert(v.retArg(300) == 300.0);
Algebraic implementation. For more portable code, it is higly recommeded to don't use this template directly. Instead, please use of Variant and Nullable, which sort types.