import optional.optional: some, no; auto opt0 = no!int; auto opt1 = some(1); // Get or optional assert(opt0.frontOr(789) == 789); assert(opt1.frontOr(789) == 1); // Lambdas () @nogc { assert(opt0.frontOr!(() => 789) == 789); assert(opt1.frontOr!(() => 789) == 1); }(); // Same with arrays/ranges int[] arr0; int[] arr1 = [1, 2]; // Get frontOr optional assert(arr0.frontOr(789) == 789); assert(arr1.frontOr(789) == 1); // Lambdas () @nogc { assert(arr0.frontOr!(() => 789) == 789); assert(arr1.frontOr!(() => 789) == 1); }();
If value is valid, it returns the internal value. This means .front for a range, .get for a Nullable!T, etc. 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.