match_case 0.0.3 copy "match_case: ^0.0.3" to clipboard
match_case: ^0.0.3 copied to clipboard

outdated

A dart package for pattern matching.

A dart package for pattern matching. Inspired by match in Rust and when in Kotlin

Usage #

var x = 11;
var result = match(
  x,
  {
    eq(1): () => "Its a one",
    eq(2): () => "Its a $x",
    gt(2) & lt(10): () => "Greater than 2",
    (digit) => digit == 11: () => "Its an eleven",
  },
  "Error!!",
);

expect(result, "Its an eleven");

match Definition #

U match<T, U>(
  T value, // Value that needs to be matched
  Map<bool Function(T), U Function()> fns,
  U defaultValue, // Value that is returned when nothing is matched (Can be null as well)
)

List of helper matcher function #

1. eq(T value)
2. gt(num number)
3. lt(num number)
3. gte(num number)
4. lte(num number)
5. range(num from, num to)

List of operators #

1. &
2. |