An Overview of Dart, Part 1
I recently wrote an article about the growing use of JavaScript as a compiler target, rather than a language that people write directly. In this article, I'm going to take a look at a system that does exactly that: Dart, a new web programming language from Google.
It's worth noting that compiling to JavaScript is currently just an implementation detail of Dart, and something that allows for rapid deployment. This is not a new idea in programming language development.
For example, early versions of Haskell generated C code, and the first version of Erlang was written as an evaluator in Prolog (which is why Erlang syntax is very similar to Prolog).
There are advantages and disadvantages to this approach. The obvious advantage is that you get something that works quickly. The disadvantage is that you are limited by the capabilities of the target language.
There is now a VM designed especially for Dart, which avoids the need for this translation, and which is shipped in some custom builds of Chromium. JavaScript code can detect the presence of this VM at run time, allowing you to load the JavaScript translation or the original Dart code, depending on the client's capabilities.
Overview
The feel of Dart is that of a language created by JavaScript developers who wished that they could be writing Java code. It does inherit some notions from C++ and Smalltalk, but it also loses some of the features of JavaScript.
Somewhat interestingly, most of the things that are lost from JavaScript are the things that make JavaScript difficult to compiler. This is not entirely surprising, from a company that employs a fairly large team working on an optimizing JavaScript compiler. There is also some cross-pollination from another Google language: Go (also the subject of my latest book).
Dart eschews the prototype-based model of JavaScript in favor of a more traditional class-based model. This will no doubt make the V8 team very happy because a large part of their work involves mapping JavaScript to a class-based model where optimizations are significantly easier.
In terms of abstract model, Dart has a lot of similarities with Smalltalk. They have a similar class and memory model, for example, although Dart classes are not first-class objects in quite the same sense as Smalltalk (for example, you cannot override static methods, the Dart equivalent of class methods). The syntax is heavily inspired by Java and C++, although the worst parts of C++ are thankfully avoided.
The Syntax
The syntax of Dart is somewhat interesting. For example, it lacks semicolon insertion. Every line must be terminated by a semicolon. JavaScript provides this feature, but does it in a way that makes the language ambiguous in a few corner cases. Go provides it in a very clean way. In fact, most modern languages omit explicit statement terminators or separators—something older languages needed largely to make parsing easier—making Dart feel slightly archaic for a new language.
There are also some odd choices in terminology. For example, Java and C++ refer to elements in an object as fields, which are either object fields or static fields. Smalltalk languages refer to these as instance variables, and correspondingly have class variables that are part of the class instead of the instances. Dart talks about instance variables and static variables. This is somewhat indicative of trying to shoehorn two mental models into the same language.
The rest of the syntax feels a little bit like this at times, providing multiple ways of doing the same thing, as if someone had just taken Smalltalk, C++, and JavaScript; thrown them in a blender; and skimmed off whatever floated to the top to create a new language.