// main.dart
class SomeClass {
Object someField;
SomeClass() {
[1].forEach((o) => someMethod());
someField = new Object();
}
void someMethod() {
if (someField != null) {
print("FAIL");
} else {
print("PASS");
}
}
}
void main() {
new SomeClass();
}
$ dart main.dart
PASS
However, if you run the generated dart2js output in a browser:
$ dart2js main.dart
Dart file (main.dart) compiled to JavaScript: out.js
$ cat '<script src="out.js"></script>' > main.html
$ # Open main.html in e.g. Google Chrome and watch the JavaScript console => it says FAIL
You get the "FAIL" output. Surely it is a bug when dart and dart2js produces different output? When looking at the generated JavaScript, it seems as if dart2js has optimized away the null check for some reason. Interestingly, if you replace this line:
[1].forEach((o) => someMethod());
with this line:
someMethod();
You get "PASS" in both cases.
I'm running Dart 1.6.0 on Mac OS X 10.9.4:
$ dart --version
Dart VM version: 1.6.0 (Tue Aug 26 22:30:33 2014) on "macos_x64"
$ dart2js --version
Dart-to-JavaScript compiler (dart2js) version: 1.6.0
Are you aware of this bug?
BR,
Martin
To unsubscribe from this group and stop receiving emails from it, send an email to compiler-dev...@dartlang.org.