Algebraic

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.

struct Algebraic (
_Types...
) {}

Constructors

this
this()
Undocumented in source.
this
this(Algebraic!RhsTypes rhs)

Construct an algebraic type from its subset.

this
this(Algebraic rhs)
Undocumented in source.
this
this(Algebraic rhs)
Undocumented in source.
this
this(Algebraic rhs)
Undocumented in source.
this
this(Algebraic rhs)
Undocumented in source.
this
this(Algebraic rhs)
Undocumented in source.
this
this(Algebraic rhs)
Undocumented in source.
this
this(Args args)
Undocumented in source.
this
this(T value)
this
this(int value)
Undocumented in source.
this
this(int value)
Undocumented in source.
this
this(int value)
Undocumented in source.
this
this(uint value)
Undocumented in source.
this
this(uint value)
Undocumented in source.
this
this(uint value)
Undocumented in source.

Destructor

~this
~this()
Undocumented in source.

Postblit

this(this)
this(this)
Undocumented in source.

Members

Aliases

AllowedTypes
alias AllowedTypes = AliasSeq!(ReplaceTypeUnless!(isVariant, This, Algebraic!_Types, _UntaggedThisTypeSetList))

Allowed types list

Kind
alias Kind = _ID_
Undocumented in source.
_ID_
alias _ID_ = uint
Undocumented in source.
get
alias get(Kind kind) = get!(AllowedTypes[kind])

get overload that accept .Algebraic.Kind.

trustedGet
alias trustedGet(Kind kind) = trustedGet!(AllowedTypes[kind])

trustedGet overload that accept .Algebraic.Kind.

Enums

Kind
enum Kind

Algebraic Kind.

Functions

_is
bool _is()

Checks if the underlaying type is an element of a user provided type set.

_is
bool _is()

Checks if the storage stores an allowed type.

get
auto get()

Defined if the first type is typeof(null)

get
inout(AllowedTypes[1]) get()

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.

get
auto ref get()

Gets an algebraic subset.

get
auto ref get()
isNull
bool isNull()

Defined if the first type is typeof(null)

kind
Kind kind()
nullify
void nullify()

Defined if the first type is typeof(null)

opAssign
ref opAssign(Algebraic!RhsTypes rhs)
opAssign
ref opAssign(T rhs)
opAssign
ref opAssign(int rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opAssign
ref opAssign(uint rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opCast
bool opCast()
opCast
Algebraic opCast()
opCmp
auto opCmp(typeof(this) rhs)
opCmp
auto opCmp(T rhs)
opCmp
auto opCmp(int rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opCmp
auto opCmp(uint rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(typeof(this) rhs)
opEquals
auto opEquals(T rhs)
opEquals
auto opEquals(int rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
auto opEquals(uint rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
toHash
size_t toHash()
toString
string toString()
void toString(W w)

Requires mir-algorithm package

trustedGet
auto ref trustedGet()

nothrow .Algebraic.get alternative that returns an algebraic subset.

trustedGet
auto ref trustedGet()

Zero cost always nothrow get alternative

Properties

get
inout(AllowedTypes[1]) get [@property setter]

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.

Static functions

_void
Algebraic _void()

Defined if AllowedTypes contains void

Templates

get
template get(RetTypes...)

Gets an algebraic subset.

trustedGet
template trustedGet(RetTypes...)

nothrow .Algebraic.get alternative that returns an algebraic subset.

Variables

_identifier_
_ID_ _identifier_;
Undocumented in source.
_identifier_
enum _ID_ _identifier_;
Undocumented in source.

Examples

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);

Meta