A dart package for pattern matching. Inspired by match
in Rust and when
in Kotlin
Usage
var x = 2;
var result = match(
x,
{
1: () => "Its a one",
() => x == 2 || x == 3: () => "Its a $x",
},
"Error!!", // (Optional) default value in case nothing is matched
);
// Output
$ "Its a 2"
match
Definition
U match<T, U>(
T value, // Value that needs to be matched
Map<T, U Function()> fns, // T -> Any primitive values or a function that returns true or false
U defaultValue, // Optional value that is returned when nothing is matched
)