Skip to content

Instantly share code, notes, and snippets.

Created December 31, 2015 13:06
Show Gist options
  • Select an option

  • Save anonymous/f30d5959d6d8a3afb6ae to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/f30d5959d6d8a3afb6ae to your computer and use it in GitHub Desktop.
plain-grass-6880

plain-grass-6880

Dart 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 runnable version here.

Created with <3 with dartpad.dartlang.org.

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