1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
@safe unittest
{
import std.sumtype;
import std.math.operations : isClose;
struct Fahrenheit { double degrees; }
struct Celsius { double degrees; }
struct Kelvin { double degrees; }
alias Temperature = SumType!(Fahrenheit, Celsius, Kelvin);
// Construct from any of the member types.
Temperature t1 = Fahrenheit(98.6);
Temperature t2 = Celsius(100);
Temperature t3 = Kelvin(273);
// Use pattern matching to access the value.
Fahrenheit toFahrenheit(Temperature t)
{
return Fahrenheit(
t.match!(
(Fahrenheit f) => f.degrees,
(Celsius c) => c.degrees * 9.0/5 + 32,
(Kelvin k) => k.degrees * 9.0/5 - 459.4
)
);
}
assert(toFahrenheit(t1).degrees.isClose(98.6));
assert(toFahrenheit(t2).degrees.isClose(212));
assert(toFahrenheit(t3).degrees.isClose(32));
// Use ref to modify the value in place.
void freeze(ref Temperature t)
{
t.match!(
(ref Fahrenheit f) => f.degrees = 32,
(ref Celsius c) => c.degrees = 0,
(ref Kelvin k) => k.degrees = 273
);
}
freeze(t1);
assert(toFahrenheit(t1).degrees.isClose(32));
// Use a catch-all handler to give a default result.
bool isFahrenheit(Temperature t)
{
return t.match!(
(Fahrenheit f) => true,
_ => false
);
}
assert(isFahrenheit(t1));
assert(!isFahrenheit(t2));
assert(!isFahrenheit(t3));
}
@safe unittest
{
import std.sumtype;
alias ExampleSumType = SumType!(int, string, double);
ExampleSumType a = 123;
ExampleSumType b = "hello";
ExampleSumType c = 3.14;
assert(a.handle == "got an int");
assert(b.handle == "got a string");
assert(c.handle == "got a double");
}
@system unittest
{
import std.sumtype;
import std.functional : partial;
import std.traits : EnumMembers;
import std.typecons : Tuple;
enum Op : string
{
Plus = "+",
Minus = "-",
Times = "*",
Div = "/"
}
// An expression is either
// - a number,
// - a variable, or
// - a binary operation combining two sub-expressions.
alias Expr = SumType!(
double,
string,
Tuple!(Op, "op", This*, "lhs", This*, "rhs")
);
// Shorthand for Tuple!(Op, "op", Expr*, "lhs", Expr*, "rhs"),
// the Tuple type above with Expr substituted for This.
alias BinOp = Expr.Types[2];
// Factory function for number expressions
Expr* num(double value)
{
return new Expr(value);
}
// Factory function for variable expressions
Expr* var(string name)
{
return new Expr(name);
}
// Factory function for binary operation expressions
Expr* binOp(Op op, Expr* lhs, Expr* rhs)
{
return new Expr(BinOp(op, lhs, rhs));
}
// Convenience wrappers for creating BinOp expressions
alias sum = partial!(binOp, Op.Plus);
alias diff = partial!(binOp, Op.Minus);
alias prod = partial!(binOp, Op.Times);
alias quot = partial!(binOp, Op.Div);
// Evaluate expr, looking up variables in env
double eval(Expr expr, double[string] env)
{
return expr.match!(
(double num) => num,
(string var) => env[var],
(BinOp bop)
{
double lhs = eval(*bop.lhs, env);
double rhs = eval(*bop.rhs, env);
final switch (bop.op)
{
static foreach (op; EnumMembers!Op)
{
case op:
return mixin("lhs" ~ op ~ "rhs");
}
}
}
);
}
// Return a "pretty-printed" representation of expr
string pprint(Expr expr)
{
import std.format : format;
return expr.match!(
(double num) => "%g".format(num),
(string var) => var,
(BinOp bop) => "(%s %s %s)".format(
pprint(*bop.lhs),
cast(string) bop.op,
pprint(*bop.rhs)
)
);
}
Expr* myExpr = sum(var("a"), prod(num(2), var("b")));
double[string] myEnv = ["a":3, "b":4, "c":7];
assert(eval(*myExpr, myEnv) == 11);
assert(pprint(*myExpr) == "(a + (2 * b))");
}
@safe unittest
{
import std.sumtype;
static struct ConvertsToSumType
{
SumType!int payload;
alias payload this;
}
static struct ContainsSumType
{
SumType!int payload;
}
assert(isSumType!(SumType!int));
assert(isSumType!ConvertsToSumType);
assert(!isSumType!ContainsSumType);
}
@safe unittest
{
import std.sumtype;
alias Number = SumType!(double, int);
Number x;
// Problem: because int implicitly converts to double, the double
// handler is used for both types, and the int handler never matches.
assert(!__traits(compiles,
x.match!(
(double d) => "got double",
(int n) => "got int"
)
));
// Solution 1: put the handler for the "more specialized" type (in this
// case, int) before the handler for the type it converts to.
assert(__traits(compiles,
x.match!(
(int n) => "got int",
(double d) => "got double"
)
));
// Solution 2: use a template that only accepts the exact type it's
// supposed to match, instead of any type that implicitly converts to it.
alias exactly(T, alias fun) = function (arg)
{
static assert(is(typeof(arg) == T));
return fun(arg);
};
// Now, even if we put the double handler first, it will only be used for
// doubles, not ints.
assert(__traits(compiles,
x.match!(
exactly!(double, d => "got double"),
exactly!(int, n => "got int")
)
));
}
@safe unittest
{
import std.sumtype;
struct Point2D { double x, y; }
struct Point3D { double x, y, z; }
alias Point = SumType!(Point2D, Point3D);
version (none)
{
// This function works, but the code is ugly and repetitive.
// It uses three separate calls to match!
@safe pure nothrow @nogc
bool sameDimensions(Point p1, Point p2)
{
return p1.match!(
(Point2D _) => p2.match!(
(Point2D _) => true,
_ => false
),
(Point3D _) => p2.match!(
(Point3D _) => true,
_ => false
)
);
}
}
// This version is much nicer.
@safe pure nothrow @nogc
bool sameDimensions(Point p1, Point p2)
{
alias doMatch = match!(
(Point2D _1, Point2D _2) => true,
(Point3D _1, Point3D _2) => true,
(_1, _2) => false
);
return doMatch(p1, p2);
}
Point a = Point2D(1, 2);
Point b = Point2D(3, 4);
Point c = Point3D(5, 6, 7);
Point d = Point3D(8, 9, 0);
assert( sameDimensions(a, b));
assert( sameDimensions(c, d));
assert(!sameDimensions(a, c));
assert(!sameDimensions(d, b));
}
@safe unittest
{
import std.sumtype;
alias handleInt = (int i) => "got an int";
assert( canMatch!(handleInt, int));
assert(!canMatch!(handleInt, string));
}
@safe unittest
{
import std.sumtype;
SumType!(string, double) example = "hello";
assert( example.has!string);
assert(!example.has!double);
// If T isn't part of the SumType, has!T will always return false.
assert(!example.has!int);
}
@safe unittest
{
import std.sumtype;
alias Example = SumType!(string, double);
Example m = "mutable";
const Example c = "const";
immutable Example i = "immutable";
assert( m.has!string);
assert(!m.has!(const(string)));
assert(!m.has!(immutable(string)));
assert(!c.has!string);
assert( c.has!(const(string)));
assert(!c.has!(immutable(string)));
assert(!i.has!string);
assert(!i.has!(const(string)));
assert( i.has!(immutable(string)));
}
@safe unittest
{
import std.sumtype;
import std.algorithm.iteration : filter;
import std.algorithm.comparison : equal;
alias Example = SumType!(string, double);
auto arr = [
Example("foo"),
Example(0),
Example("bar"),
Example(1),
Example(2),
Example("baz")
];
auto strings = arr.filter!(has!string);
auto nums = arr.filter!(has!double);
assert(strings.equal([Example("foo"), Example("bar"), Example("baz")]));
assert(nums.equal([Example(0), Example(1), Example(2)]));
}
@safe unittest
{
import std.sumtype;
SumType!(string, double) example1 = "hello";
SumType!(string, double) example2 = 3.14;
assert(example1.get!string == "hello");
assert(example2.get!double == 3.14);
}
@safe unittest
{
import std.sumtype;
alias Example = SumType!(string, double);
Example m = "mutable";
const(Example) c = "const";
immutable(Example) i = "immutable";
assert(m.get!string == "mutable");
assert(c.get!(const(string)) == "const");
assert(i.get!(immutable(string)) == "immutable");
}
@safe unittest
{
import std.sumtype;
import std.algorithm.iteration : map;
import std.algorithm.comparison : equal;
alias Example = SumType!(string, double);
auto arr = [Example(0), Example(1), Example(2)];
auto values = arr.map!(get!double);
assert(values.equal([0, 1, 2]));
}
@safe unittest
{
import std.sumtype;
SumType!(string, double) example = "hello";
assert(example.tryGet!string == "hello");
double result = double.nan;
try
result = example.tryGet!double;
catch (MatchException e)
result = 0;
// Exception was thrown
assert(result == 0);
}
@safe unittest
{
import std.sumtype;
import std.exception : assertThrown;
const(SumType!(string, double)) example = "const";
// Qualifier mismatch; throws exception
assertThrown!MatchException(example.tryGet!string);
// Qualifier matches; no exception
assert(example.tryGet!(const(string)) == "const");
}
@safe unittest
{
import std.sumtype;
import std.algorithm.iteration : map, sum;
import std.functional : pipe;
import std.exception : assertThrown;
alias Example = SumType!(string, double);
auto arr1 = [Example(0), Example(1), Example(2)];
auto arr2 = [Example("foo"), Example("bar"), Example("baz")];
alias trySum = pipe!(map!(tryGet!double), sum);
assert(trySum(arr1) == 0 + 1 + 2);
assertThrown!MatchException(trySum(arr2));
}
|