AlgebraicException

Algebraic type exception.

@safe pure static
class AlgebraicException : Exception {}

Examples

equality and comparison

1 Algebraic!(float) a, b;
2 static assert(a.hasFixedSize);
3 
4 dbg(a.as!float);
5 a = 1.0f;
6 dbg(a.as!float);
7 assert(a._tix != a.Ix.init);
8 
9 dbg(b.as!float);
10 b = 1.0f;
11 dbg(b.as!float);
12 assert(b._tix != b.Ix.init);
13 
14 assert(a._tix == b._tix);
15 assert((cast(ubyte*)&a)[0 .. a.sizeof] == (cast(ubyte*)&b)[0 .. b.sizeof]);
16 assert(a == b);             // TODO this errors with dmd master
1 alias C = Algebraic!(float, double);
2 C a = 1.0;
3 const C b = 2.0;
4 const C c = 2.0f;
5 const C d = 1.0f;
6 
7 assert(a.commonValue == 1);
8 assert(b.commonValue == 2);
9 assert(c.commonValue == 2);
10 assert(d.commonValue == 1);
11 
12 // nothrow comparison possible
13 assert(a < b);
14 assert(a < c);
15 assert(a == d);
16 
17 static assert(!a.hasFixedSize);
18 static assert(a.allowsAssignmentFrom!float);
19 static assert(a.allowsAssignmentFrom!double);
20 static assert(!a.allowsAssignmentFrom!string);
21 
22 a.clear();
23 assert(!a.hasValue);
24 assert(a.peek!float is null);
25 assert(a.peek!double is null);
26 assert(a.currentSize == 0);

aliasing traits

import std.traits : hasAliasing;
static assert(!hasAliasing!(Algebraic!(long, double)));
static assert(!hasAliasing!(Algebraic!(long, string)));
static assert(!hasAliasing!(Algebraic!(long, immutable(double)*)));
static assert(hasAliasing!(Algebraic!(long, double*)));

equality and comparison

Algebraic!(int) a, b;
static assert(a.hasFixedSize);
a = 1;
b = 1;
assert(a == b);

equality and comparison

Algebraic!(float) a, b;
static assert(a.hasFixedSize);
a = 1.0f;
b = 1.0f;
assert(a == b);

equality and comparison

1 Algebraic!(float, double, string) a, b;
2 
3 static assert(!a.hasFixedSize);
4 
5 a = 1.0f;
6 b = 1.0f;
7 assert(a == b);
8 
9 a = 1.0f;
10 b = 2.0f;
11 assert(a != b);
12 assert(a < b);
13 assert(b > a);
14 
15 a = "alpha";
16 b = "alpha";
17 assert(a == b);
18 
19 a = "a";
20 b = "b";
21 assert(a != b);
22 assert(a < b);
23 assert(b > a);

AA keys

alias C = Algebraic!(float, double);
static assert(!C.hasFixedSize);
string[C] a;
a[C(1.0f)] = "1.0f";
a[C(2.0)] = "2.0";
assert(a[C(1.0f)] == "1.0f");
assert(a[C(2.0)] == "2.0");

verify nothrow comparisons

alias C = Algebraic!(int, float, double);
static assert(!C.hasFixedSize);
assert(C(1.0) < 2);
assert(C(1.0) < 2.0);
assert(C(1.0) < 2.0);
static assert(!__traits(compiles, { C(1.0) < 'a'; })); // cannot compare with char
static assert(!__traits(compiles, { C(1.0) < "a"; })); // cannot compare with string

TODO

// alias C = Algebraic!(int, float, double);
// alias D = Algebraic!(float, double);
// assert(C(1) < D(2.0));
// assert(C(1) < D(1.0));
// static assert(!__traits(compiles, { C(1.0) < "a"; })); // cannot compare with string

if types have CommonType comparison is nothrow @nogc

alias C = Algebraic!(short, int, long, float, double);
static assert(!C.hasFixedSize);
assert(C(1) != C(2.0));
assert(C(1) == C(1.0));

if types have CommonType then comparison is nothrow @nogc

alias C = Algebraic!(short, int, long, float, double);
static assert(!C.hasFixedSize);
assert(C(1) != C(2.0));
assert(C(1) == C(1.0));
1 import nxt.fixed_array : MutableStringN;
2 alias String15 = MutableStringN!(15);
3 
4 String15 s;
5 String15 t = s;
6 assert(t == s);
7 
8 alias V = Algebraic!(String15, string);
9 V v = String15("first");
10 assert(v.peek!String15);
11 assert(!v.peek!string);
12 
13 v = String15("second");
14 assert(v.peek!String15);
15 assert(!v.peek!string);
16 
17 v = "third";
18 assert(!v.peek!String15);
19 assert(v.peek!string);
20 
21 auto w = v;
22 assert(v == w);
23 w.clear();
24 assert(!v.isNull);
25 assert(w.isNull);
26 w = v;
27 assert(!w.isNull);
28 
29 v = V.init;
30 assert(v == V.init);

check default values

import nxt.fixed_array : MutableStringN;
alias String15 = MutableStringN!(15);

alias V = Algebraic!(String15, string);
V _;
assert(_._tix == V.Ix.init);
assert(V.init._tix == V.Ix.init);

// TODO import nxt.bit_traits : isInitAllZeroBits;
// TODO static assert(isInitAllZeroBits!(V));

Meta