Last active
January 23, 2023 06:21
-
-
Save nythrox/72280323ae84a9f823309776b86b1518 to your computer and use it in GitHub Desktop.
Flutter/Dart pattern matching
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
void main() { | |
Greeting message = Hi(); | |
final result = match(message, { | |
on((Hi hi) => "hello, ${hi.name}"), | |
on((Bye bye) => "bye, see you at ${bye.seeYouAgain}"), | |
otherwise(() => throw Error()), | |
}); | |
print(result); | |
} | |
abstract class Greeting {} | |
class Hi implements Greeting { | |
String name; | |
} | |
class Bye implements Greeting { | |
DateTime seeYouAgain; | |
} |
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
void main2() { | |
final future = ObservableFuture.value(10); | |
final futureResult = match(future.status, { | |
when(FutureStatus.fulfilled, () => Text("done ${future.value}")), | |
when(FutureStatus.pending, () => CircularProgressIndicator()), | |
when(FutureStatus.rejected, () => Text("error ${future.error})), | |
}); | |
runApp(futureResult); | |
} |
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
abstract class MatchingActions<T, R> {} | |
class Otherwise<T, R> implements MatchingActions<T, R> { | |
final R Function() onMatched; | |
Otherwise(this.onMatched); | |
} | |
class On<T, R> implements MatchingActions<T, R> { | |
final R Function(T type) onMatched; | |
Type matchesType; | |
On(this.onMatched) : matchesType = T; | |
} | |
class OnValue<T, R> implements MatchingActions<T, R> { | |
final R Function() onMatched; | |
final T value; | |
OnValue(this.value, this.onMatched); | |
} | |
MatchingActions<T, R> on<T, R>(R Function(T type) onMatched) { | |
return On<T, R>(onMatched); | |
} | |
MatchingActions<T, R> when<T, R>(T value, R Function() onMatched) { | |
return OnValue<T, R>(value, onMatched); | |
} | |
MatchingActions<Null, R> otherwise<R>(R Function() onMatched) { | |
return Otherwise<Null, R>(onMatched); | |
} | |
R match<T, R, U extends T>(T value, Set<MatchingActions<U, R>> matchers) { | |
Otherwise _otherwise; | |
for (final matcher in matchers) { | |
if (matcher is On && value.runtimeType == (matcher as On).matchesType) { | |
return (matcher as On).onMatched(value); | |
} | |
if (matcher is OnValue && (matcher as OnValue).value == value) { | |
return (matcher as OnValue).onMatched(); | |
} | |
} | |
return (_otherwise.onMatched()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment