Dart by Example: Initializer Lists

1. import 'dart:math';

3. class Position {
4. final int x;
5. final int y;
6. final double rad;

8. // An initializer list allows
9. // fields to be defined before the constructor body.
10. // This is required for final fields.
11. Position(int x, int y)
12. : this.x = x,
13. this.y = y,
14. rad = atan2(y, x);
15. }

17. main() {
18. var p = new Position(2, 3);
19. print('x: ${p.x} y: ${p.y}');
20. print('rad: ${p.rad}');
21. }
1. $ initializer_lists.dart
2. x: 2 y: 3
3. rad: 0.5880026035475675

by @jryanio | source | license