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

A dart package for pattern matching.

Status

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",
  },
  other: val("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, {
  required U Function() other,  // a method to return the default value
})

List of helper matcher function #

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

List of helper function for other param #

1. val(T value) // Function that returns the passed value
2. nil<T>() // Function that returns null

List of operators #

1. &
2. |