https://github.com/fujidaiti/dart_just_match
A naive pattern matching for Dart
https://github.com/fujidaiti/dart_just_match
Last synced: 3 months ago
JSON representation
A naive pattern matching for Dart
- Host: GitHub
- URL: https://github.com/fujidaiti/dart_just_match
- Owner: fujidaiti
- License: mit
- Created: 2020-12-26T10:07:01.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-27T15:53:26.000Z (almost 5 years ago)
- Last Synced: 2025-02-21T19:26:04.847Z (8 months ago)
- Language: Dart
- Homepage: https://pub.dev/packages/just_match
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# just_match
`just_match` *just* provides a naive pattern *match*ing for Dart.
```dart
import 'package:just_match/just_match.dart';
import 'package:just_match/just_match_ext.dart';void main() {
print(interpret(100)); // 'The value is 100'
print(interpret(Bar())); // 'The value is a Bar'
print(interpret('Bob')); // "I'm sorry, I can't understand"
}String interpret(Object value) {
return value.match([
'3.14'.then((it) => 'The value is pi'),
100.then((it) => 'The value is 100'),
Bar.then((it) => 'The value is a Bar'),
isValidUserName.then((it) => 'The value is a valid user name'),
otherwise(() => "I'm sorry, I can't understand"),
]);
}class Bar {
static Case then(Action action) => type(action);
}bool isValidUserName(String value) =>
4 < value.length && value.length < 10;```