Dart, a language from Google to replace JavaScript and PHP
Dart runs in the browser and on the server, it provides the features of a very classical application programming language.
The new language from Google is intended to be more structured like Pascal at one time against Basic. Available at dartlang.org, it offers an alternative to JavaScript in the browser but also on the server or for native code in the browser.
It implements concurrency in the form of communicating actors with their own environment and has true classes.
Faced with fundamental design problems in JavaScript that can not be solved by incremental improvements, Google opted for a radical solution: completely replaces the language with another, with a syntax that is between that of JavaScript and Java. But JavaScript is a language that is interesting in its overall design, it is the details that are weak and Dart unfortunately change the overall design to return to the classics.
Another solution would have beem better: to allow browsers to use any programming language. Or use a bytecode for which we could create different languages in frontend.
It is unlikely that Microsoft or Apple adopt this language before a long time, so the virtual machine is confined to Chrome. In fact Microsoft chose a different option: to develop TypeScript, a language that is compiled to JavaScript.
The syntax of Dart is more than classic, it's actually a language of the 80. Scriptol dating from 2001 was already more innovative. However, Dart will bring a progress with concurrency.
Google promises that programming will become interactive thanks to tools, you will edit and run a program directly, and change the code according to the results. For a Web application, it will be a revolution.
Programs may be evolving. You can begin with a simple script with dynamic variables, which can be transformed into software when you add typed variables and classes.
Another advantage is the ability to use the same language on the server and the browser, which simplifies programming, but we can already do that with Node.js (and the Google's V8 compiler).
Dart will be in competition with Harmony, the final version of JavaScript in development which will also include classes. A Cloud IDE named Brighty will help to develop web applications. It will work with Dart or JavaScript.
Dart Features
A Dart program can be executed by a virtual machine or compiled into JavaScript and thus produce a code usable by all browsers.
On Chrome, a plugin will use the virtual machine.
- The main function executes the code after loading, and not step by step while the page is parsed as JavaScript.
- Classes and interfaces.
Unlike JavaScript, in Dart you can declare classes rather than dynamic objects, and have single inheritance.
It does not allow overloading methods, and uses a complicated alternative for constructors. - A class inherits from another with the extends keyword. See example below.
- Fonctions.
A function is expressed as in C (and not with the function keyword as JavaScript), except that the return type is optional. If not specified, it is dynamic.
As in JavaScript, functions may be embedded into another functon. - It can use libraries, ie already compiled code.
- Optional types.
The programmer can use dynamic variables to a script or typed variable for security on a larger project or to speed up the processing. Note that type inference by modern JavaScript compilers withdraws this advantage. - Concurrency.
Entities named Isolates can operate concurrently. They communicate by messages but have their own memory space.
So the language is not multi-threaded, but has parallel processes, actors. - The + symbol is used for the addition or concatenation. It can also be overridden by class methods.
- Facilitated self-documentation.
- Like in Scriptol, a name should not be declared locally within a block when it already exists in a containing block (here you get a warning, in Scriptol this a syntax error).
- An object is public (used outside the library), or private, in this case it is prefixed by an underscore.
- Classes can include getters and setters. The syntax is:
type name => expression. - A method can be static. It is then declared with the static prefix. The same applies to attributes.
- Operators are those of PHP. This includes === and !== for strict comparisons.
- Reserved words:
abstract, asset, wagon, box, catch, class, continue, do, except, extends, factory, false, finally, for, get, if, implements, import, interface, in, include, is, library, negate, operator, proxy, set, source, static, switch, throw, try, true, typedef, until, while. - List of types and builtin objects:
bool, Date, Double, Duration, int, list, map, number, Isolate, Object, Match, Math, Pattern, Queue, RegExp, String, var, void. - A for loop can be used to scan a list.
- Snapshot
A snapshot is a form of persistence of the state of a program. Its data are stored on disk for use at the next session. This allows you to instantly restart a program as if it was in standby. (The current documentation does not mention it but the snapshot code is part of the source of the virtual machine).
We see that there is really nothing innovative in all this, nothing but a kind of upgrading to norms of yesteryear of JavaScript, a language created in the 90s, or a modern version of C. The language is not uninteresting and offers powerful features, but really lacks imagination.
Example of code
Hello:
main()
{
print("Hello World!");
}
Function:
String catstr(String str)
{
String x = "Message : $str";
return x;
}
print(catstr("Hello"));
We can concatenate two variables a and b with: String x = "$a$b";
Objects:
Class Vehicle
{
num fuel;
Vehicule(num this.vitesse, num this.passengers);
num distance() // the type is optional.
{
print fuel / this.passengers;
}
}
Class Car extends Vehicle
{
...
}
Dart will it be supported on all browsers?
There is a version of Chrome which integrates a Dart virtual machine called Dartium. However, the community that develops Webkit refused its implementation on the ground that is not part of the standard Web. It's the same thing on the side of Mozilla where one believes that a future version of JavaScript could add the same improvements that brings Dart, mainly the class model and typed variables (without losing the advantages of JavaScript). We do not mean that Microsoft has issued a negative opinion on the subject, or that Apple is far from wanting to support Google.
It seems that in the near future, Dart will only work on Chrome and since it runs on Android, provides an alternative to native programming. But one can imagine a Dart plugin for other browser, such as Google Frame that enables support for HTML 5 on older version of Internet Explorer.
Documents and resources
- Dartlang.org. Description and specification of the language.
- Point by point comparison of Dart and JavaScript. Provided by Google.
- Try Dart. Enter code online to test it.
- Criticism of Dart. From a Perl programmer.
- JDart. A version that compiles Dart code into bytecode for the Java virtual machine.
See also...
- JavaScript and Harmony. The evolution of the language. The version 6 of ECMAScript brings most of Dart's features.
- How not to create a new programming language. The eternal C syntax is it the best one?
