match<A> function

A Function(bool) match<A>(
  1. LazyArg<A> onFalse,
  2. LazyArg<A> onTrue
)

Returns a function that depending on the provided bool value, invokes and returns the result of either onFalse or onTrue function. Both onFalse and onTrue must return the same type A.

Example:

void main() {
  int value = 42;

  // Define two lazy functions for onFalse and onTrue cases
  LazyArg<int> onFalseFunc = () {
    print("Value is false");
    return 0;
  };

  LazyArg<int> onTrueFunc = () {
    print("Value is true");
    return 100;
  };

  // Create a match function with onFalseFunc and onTrueFunc
  final matcher = match(onFalseFunc, onTrueFunc);

  // Use the matcher to get the result based on the bool value
  int result1 = matcher(false); // Output: Value is false
  int result2 = matcher(true); // Output: Value is true

  print(result1); // Output: 0
  print(result2); // Output: 100
}

Implementation

A Function(bool) match<A>(LazyArg<A> onFalse, LazyArg<A> onTrue) {
  return (bool value) => value ? onTrue() : onFalse();
}