matchW<A, B> function

Object? Function(bool) matchW<A, B>(
  1. LazyArg<A> onFalse,
  2. LazyArg<B> onTrue
)

Returns a function that depending on the provided bool value, invokes and returns the result of either onFalse or onTrue function. The return type is Object? because onFalse and onTrue can potentially return different types.

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<String> onTrueFunc = () {
    print("Value is true");
    return "Hello";
  };

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

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

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

Implementation

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