Function frontOr
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.
auto frontOr(alias elsePred, T)
(
auto ref T value
);
auto frontOr(T, U)
(
auto ref T value,
lazy U elseValue
);
elsePred
can return void as well, in which case frontOr also returns void.
Parameters
Name | Description |
---|---|
value | the value to check |
elseValue | the value to get if value is invalid |
elsePred | the perdicate to call if value is invalid |
Returns
Nullable!T
: value .get
or elseValue
Optional!T
: value .front
or elseValue
Range!T
: value .front
or elseValue
Example
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);
}();