Skip to content

Commit

Permalink
[docs] doc orElse()
Browse files Browse the repository at this point in the history
  • Loading branch information
aberba committed Jul 30, 2020
1 parent 0ea7503 commit 3d608cf
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions source/nyinaa/range.d
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,9 @@ auto concreteRange(Range)(Range r)
return Array!(ElementType!Range)(r)[];
}

/// use for structs with a `present` and `value` member.
// alias orElse = (a, b) => a.present ? a.value : b;

/**
used for two values of same types (integer, strings, struct, etc) and a
custom comparison function, `cmpFunc`. `cmpFunc` is used to determine
whether first argument passes else returns second.
use for two values of the same type (integer, string, struct, etc) and a custom comparison function, `cmpFunc`. If `cmpFunc` passes it returns `value` else returns `fallback`.
Params:
value = the first value used in comparison function
Expand All @@ -69,8 +65,15 @@ T orElse(T, alias cmpFunc)(T value, T fallback)
//
unittest
{
alias compareFunc = (a) => a == 1;
assert(0.orElse!(int, compareFunc)(1) == 1);
alias compareFunc = (a) => a == 1; // do some computation

int num = 0;
assert(num.orElse!(int, compareFunc)(1) == 1);

alias compareFunc2 = (a) => a == "yes";

string data = "no"; // do some processing
assert(data.orElse!(string, compareFunc2)("yes") == "yes");
}

/** alias for .then which is useful for range concatenation
Expand Down

0 comments on commit 3d608cf

Please sign in to comment.