Skip to content

Instantly share code, notes, and snippets.

@glitchedmob
Created October 23, 2020 14:25
Show Gist options
  • Save glitchedmob/fe4db0a3d2dd4d2865ff40e810d78dcb to your computer and use it in GitHub Desktop.
Save glitchedmob/fe4db0a3d2dd4d2865ff40e810d78dcb to your computer and use it in GitHub Desktop.
typedef MatchValue<T> = T Function();
TValue match<TOptionType, TValue>(
TOptionType selectedOption,
Map<TOptionType, MatchValue<TValue>> branches, {
MatchValue<TValue> defaultValue,
}) {
if (branches.containsKey(selectedOption)) {
return branches[selectedOption]();
}
defaultValue ??= () => null;
return defaultValue();
}
enum Status {
hello,
world,
}
void main() {
var status = Status.hello;
match(
status,
{
Status.hello: () => print('hello'),
Status.world: () => print('world'),
},
defaultValue: () => print('Default'),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment