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.

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

  1. auto or(T value)
    or
    (
    alias elsePred
    T
    )
    (
    auto ref T value
    )
  2. auto or(T value, U elseValue)

Parameters

value T

the value to check

elsePred

the perdicate to call if value is invalid

Return Value

Type: auto
  • 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
  • Examples

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

    Meta