import optional.optional: some, no; auto opt0 = no!int; auto opt1 = some(1); // Get or optional assert(opt0.or(opt1) == opt1); assert(opt1.or(opt0) == opt1); // Lambdas () @nogc { assert(opt0.or!(() => opt1) == opt1); assert(opt1.or!(() => opt0) == opt1); }(); // Same with arrays/ranges int[] arr0; int[] arr1 = [1, 2]; // Get or optional assert(arr0.or(arr1) == arr1); assert(arr1.or(arr0) == arr1); // Lambdas () @nogc { assert(arr0.or!(() => arr1) == arr1); assert(arr1.or!(() => arr0) == arr1); }();
If value is valid, it returns the value. If value is invalid, then elseValue is returned. If an elsePred is provided than that is called.
elsePred can return void as well, in which case frontOr also returns void.