4 ms·
You mean the graphical one where they use storage, forms, and events? Let's not be misleading... Hello World in Dart is: main() { print("Hello World!"); }
by WoodenChair 13y ago
You mean the graphical one where they use storage, forms, and events? Let's not be misleading... Hello World in Dart is:
main() {
print("Hello World!");
}
- nailer 13y ago> main() { print("Hello World!"); } Yes, that's my point. Why not just print("Hello World!"), like everything else?
- munificent 13y agoThe top level of a Dart program is a group of definitions, like in Java or C#, instead of a series of imperative statements. That makes it easier for static analysis, since you don't have strange constructs like: 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.
- nailer 13y agoPersonally I don't mind hoisting but for the moment, OK. So obvious question: why doesn't the language create this entry point for me?
- spankalee 13y agoBecause once you allow statements at the top level alongside declarations, which statement should be run first? main() makes that clear. 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.
- nailer 13y agoConvention? I guess it does comedown to function hoisting in the end, so the expectation is the body of code after the functions are defined / function variables created. I think a lot of people prefer that convention over boilerplate code, and the tide of language development seems to be heading in that direction. OT: script tag soup is, for first party stuff anyway, avoided with a single script for a module loader these days.
- xxgreg 13y agoOne big advantage of top-level declarations is it is possible for the VM to use a snapshot for fast start up. 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/ https://www.dartlang.org/articles/snapshots/
- munificent 13y agoIf you allowed top-level imperative code to mix with non-imperative definitions, it gets really semantically squishy. Top-level variables in Dart are lazy-initialized, which lets their order not matter. But variables in imperative code are initialized at definition time. How does the language tell which one it should prefer if it allows both at the top level? 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.
- ahoge 13y agomain()=>print('Hello World!');