Dart by Example: Initializer Lists

  1. import 'dart:math';
  2. class Position {
  3. final int x;
  4. final int y;
  5. final double rad;
  6. // An initializer list allows
  7. // fields to be defined before the constructor body.
  8. // This is required for final fields.
  9. Position(int x, int y)
  10. : this.x = x,
  11. this.y = y,
  12. rad = atan2(y, x);
  13. }
  14. main() {
  15. var p = new Position(2, 3);
  16. print('x: ${p.x} y: ${p.y}');
  17. print('rad: ${p.rad}');
  18. }
  19.  
  1. $ initializer_lists.dart
  2. x: 2 y: 3
  3. rad: 0.5880026035475675

by @jryanio | source | license