Function or

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.

auto or(alias elsePred, T) (
  auto ref T value
);

auto or(T, U) (
  auto ref T value,
  lazy U elseValue
);

elsePred can return void as well, in which case frontOr also returns void.

Parameters

NameDescription
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.isNull ? elseValue : value
  • Optional!T: value.empty ? elseValue : value
  • Range!T: value.empty ? elseValue : value
  • Null-testable type: value is null ? elseValue : value
  • Example

    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);
    }();