I have not used the Maybe library, but something like this seems to count:
import std.stdio; struct Maybe(T) { private { bool isNothing = true; T value; } void opAssign(T val) { isNothing = false; value = val; } void opAssign(Maybe!T val) { isNothing = val.isNothing; value = val.value; } T get() @property { if (!isNothing) return value; else throw new Exception("This is nothing!"); } bool hasValue() @property { return !isNothing; } } Maybe!int doSomething(in int k) { Maybe!int ret; if (k < 10) ret = 3; return ret; } void main() { auto retVal = doSomething(5); assert(retVal.hasValue); writeln(retVal.get); retVal = doSomething(15); assert(!retVal.hasValue); writeln(retVal.hasValue); }
With some overloading of the creative operator, the Maybe structure can behave quite naturally. In addition, I have templated the Maybe structure, so it can be used with any type.
source share