Dart by Example: Function Types

  1. typedef bool Validator(int n);
  2. bool positive(int n) => n >= 0;
  3. bool lessThan100(int n) => n < 100;
  4. bool bothValid(int n, Validator v1, Validator v2) {
  5. return v1(n) && v2(n);
  6. }
  7. main() {
  8. Validator both = (int n) => bothValid(n, positive, lessThan100);
  9. print('${both(5)}');
  10. print('${both(1000)}');
  11. }
  12.  
  1. $ dart typedef.dart
  2. true
  3. false

by @jryanio | source | license