Skip to content

Instantly share code, notes, and snippets.

@antonmoiseev
Forked from anonymous/main.dart
Last active December 31, 2015 14:24
Show Gist options
  • Select an option

  • Save antonmoiseev/0706c07cf795ec695731 to your computer and use it in GitHub Desktop.

Select an option

Save antonmoiseev/0706c07cf795ec695731 to your computer and use it in GitHub Desktop.
Dart: constructor initializers quiz

Does the order of the superconstructor call in the initializer list changes the invocation order of the superclass constructor and other initializers?

In other words in which order the code will be executed? Pick the right answer:

  1. B#super, A#ctor, B#bar, B#ctor
  2. B#bar, B#super, A#ctor, B#ctor
  3. B#super, B#bar, A#ctor, B#ctor
  4. B#super, B#bar, B#ctor, A#ctor

Find the answer and the runnable version here 😉

class A {
final foo;
A(this.foo) {
log('A#ctor');
}
}
class B extends A {
final bar;
B()
: super(log('B#super')),
bar = log('B#bar') {
log('B#ctor');
}
}
class C extends A {
final bar;
C()
: bar = log('C#bar'),
super(log('C#super')) {
log('C#ctor');
}
}
/// Stopwatch to timestamp the log messages.
final sw = new Stopwatch();
/// Prints the [label] with a timestamp next to it.
log(String label) => print('${label.padRight(7)}: ${sw.elapsedTicks}');
main() {
print('Experiment 1\n');
sw.start();
new B();
sw..stop()..reset();
print('\nExperiment 2\n');
sw.start();
new C();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment