Created
October 23, 2020 14:25
-
-
Save glitchedmob/fe4db0a3d2dd4d2865ff40e810d78dcb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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