The OpenD Programming Language

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 (
T__...
) {
static if(!(T__.length == 1 && is(T__[0] == union)))
enum immutable(char[][]) metaFieldNames__;
static if(!(T__.length == 1 && is(T__[0] == union)))
enum immutable(char[][]) typeFieldNames__;
static if(AllowedTypes.length > 1)
ID__ identifier__;
static if(!(AllowedTypes.length > 1))
enum ID__ identifier__;
}

Constructors

this
this(Algebraic!RhsTypes rhs)

Construct an algebraic type from its subset.

this
this(T value)

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Postblit

A postblit is present on this object, but not explicitly documented in the source.

Members

Aliases

AllowedTypes
alias AllowedTypes = AliasSeq!(ReplaceTypeUnless!(.isVariant, .This, Algebraic!T__, Types__))

Allowed types list

_is
alias _is(Kind kind) = _is!(AllowedTypes[kind])
alias _is(immutable(char)[] kind) = _is!(__traits(getMember, Kind, kind))

_is overload that accept .Algebraic.Kind.

get
alias get(Kind kind) = get!(AllowedTypes[kind])
alias get(immutable(char)[] kind) = get!(__traits(getMember, Kind, kind))

get overload that accept .Algebraic.Kind.

trustedGet
alias trustedGet(Kind kind) = trustedGet!(AllowedTypes[kind])
alias trustedGet(immutable(char)[] kind) = trustedGet!(__traits(getMember, Kind, 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)

isOk
bool isOk()

Determines if the variant holds value of some none-isVariant type. The property is avaliable only for $(ResultVariant)

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)
opCast
bool opCast()
opCast
Algebraic opCast()
opCmp
auto opCmp(UnqualRec!T rhs)
opEquals
bool opEquals(Algebraic rhs)
opEquals
bool opEquals(UnqualRec!T rhs)
opSlice
size_t[2] opSlice(size_t i, size_t j)
toHash
size_t toHash()
toString
immutable(char)[] 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

Static variables

metaFieldNames__
char[][] metaFieldNames__;
typeFieldNames__
char[][] typeFieldNames__;

Templates

get
template get(RetTypes...)

Gets an algebraic subset.

trustedGet
template trustedGet(RetTypes...)

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

Examples

Constructor and methods propagation.

1 static struct Base
2 {
3     double d;
4 }
5 
6 static class Cc
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     override size_t toHash() scope const { return hashOf(_b) ^ a; }
18 
19     string b() const @property { return _b; }
20     void b(string b) @property { _b = b; }
21 
22     int retArg(int v) { return v; }
23     string retArgT(TArgs...)(int v) { return TArgs.stringof; }
24 
25     this(int a, string b)
26     {
27         this.a = a;
28         this._b = b;
29     }
30 }
31 
32 static struct S
33 {
34     string b;
35     int a;
36 
37     double retArg(double v) { return v; }
38     double retArgT(TArgs...)(int v) { return v * TArgs.length; }
39 
40     // alias this members are supported
41     Base base;
42     alias base this;
43 }
44 
45 static void inc(ref int a) { a++; }
46 
47 alias V = Nullable!(Cc, S); // or Variant!
48 
49 auto v = V(2, "str");
50 assert(v._is!Cc);
51 assert(v.a == 2);
52 assert(v.b == "str");
53 // members are returned by reference if possible
54 inc(v.a);
55 assert(v.a == 3);
56 v.b = "s";
57 assert(v.b == "s");
58 // alias this members are supported
59 v.d = 10;
60 assert(v.d == 10);
61 // method call support
62 assert(v.retArg(100)._is!int);
63 assert(v.retArg(100) == 100);
64 
65 // method with template args support
66 assert(v.retArgT!dchar(100)._is!string);
67 assert(v.retArgT!dchar(100) == "(dchar)");
68 
69 v = V("S", 5);
70 assert(v._is!S);
71 assert(v.a == 5);
72 assert(v.b == "S");
73 // members are returned by reference if possible
74 inc(v.a);
75 assert(v.a == 6);
76 v.b = "s";
77 assert(v.b == "s");
78 // alias this members are supported
79 v.d = 15;
80 assert(v.d == 15);
81 // method call support
82 assert(v.retArg(300)._is!double);
83 assert(v.retArg(300) == 300.0);

Meta