Yes, that's my point. Why not just print("Hello World!"), like everything else?
Yes, that's my point. Why not just print("Hello World!"), like everything else?
if (itsTuesday) {
class Foo {
...
}
}
It also means mutual recursion just works, without weird semantic shenanigans like hoisting in JavaScript. In Dart, the top-level is "simultaneous": all of those definitions are static with no mixing of side-effecting imperative code at the top-level which would make ordering matter.The main side-effect of this approach is that you have to wrap your entrypoint imperative code in a main() function. It's a pretty small price to pay, especially in an application with thousands of lines of code.
In JavaScript it depends on the order of script tags and their attributes. It's really hard to determine what code is actually run at application startup, and what the initial state is.
Making the top-level only declarations means that the programmer knows what code runs first (the first line in main()) and the VM knows that the order of evaluating the declarations doesn't matter. It can evaluate them all and cache the result so that subsequent runs are fast, it can execute main() before other declarations and evaluate them on an as-needed basis, or something in between.
OT: script tag soup is, for first party stuff anyway, avoided with a single script for a module loader these days.
This is not really possible if you must interpret top level code to build up your top-level declarations, as done in JS.
Using snapshots, the Dart authors have said it should be possible to improve script start up time by 10x over V8. This is a big win for large script heavy apps.
Edit: link https://www.dartlang.org/articles/snapshots/
Also, if you import a library that contains this top-level imperative code, how do you invoke it? Is it invoked automatically? When? How does that play with cyclic imports?
The small amount of overhead of a main() method solves all of that.