xxxxxxxxxx
/+dub.sdl:
dependency "optional" version="~>0.7.1"
+/
import std.stdio;
import optional;
void main()
{
// Create empty optional
auto a = no!int;
a.writeln; // none ([])
++a; // safe
a.writeln; // still none
// Assign and try doing the same stuff
a = 9;
a.writeln; // some [9]
++a;
a.writeln; // some [10]
// Acts like a range as well
import std.algorithm : map;
alias prod = a => a * a;
some(10).map!prod.writeln; // [100]
no!int.map!prod.writeln; // empty
// safe access array
auto arr = some([1, 2, 3]);
arr[0].writeln; // [1]
arr[300].writeln; // safe, []
// safe dispatch of refs or optionals
struct Data {
int operation() { return 7; }
Data moreData() { return this; }
}
Data* data = null;
data.dispatch.moreData.operation.writeln; // safe, []
data = new Data();
data.dispatch.moreData.operation.writeln; // [7]
}