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.

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

  1. auto frontOr(T value)
    frontOr
    (
    alias elsePred
    T
    )
    (
    auto ref T value
    )
  2. auto frontOr(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.get or elseValue
  • Optional!T: value.front or elseValue
  • Range!T: value.front or elseValue
  • Examples

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

    Meta