xxxxxxxxxx
/+dub.sdl:
dependency "optional" version="~>0.7.1"
+/
import std.stdio;
import optional;
void main()
{
// Safely dispatch to whatever inner type is
struct A {
struct Inner {
int g() { return 7; }
}
Inner inner() { return Inner(); }
int f() { return 4; }
}
auto d = some(A());
// Dispatch to one of its methods
d.dispatch.f(); // calls a.f, returns some(4)
d.dispatch.inner.g(); // calls a.inner.g, returns some(7)
// Use on a pointer or reference type as well
A* e = null;
// If there's no value in the reference type, dispatching works, and produces an optional
assert(e.dispatch.f() == none);
assert(e.dispatch.inner.g() == none);
}