Dart (programming language)

Dart
Dart-logo.png
Paradigm(s) object-oriented
Appeared in 2011
Developer Google
Preview release Beta 1 (June 19, 2013; 3 months ago (2013-06-19)[1])
Typing discipline optional
Influenced by JavaScript, Java, Smalltalk, Erlang, Strongtalk, C#[2]
License BSD License
Usual filename extensions .dart
Website www.dartlang.org

Dart is an open-source Web programming language developed by Google. It was unveiled at the GOTO conference in Aarhus, 2011 October 10–12.[3] The goal of Dart is "ultimately to replace JavaScript as the lingua franca of web development on the open web platform."[4] Dart is intended to address JavaScript's problems (which Google's engineers felt could not be solved by evolving the language) while offering better performance, the ability "to be more easily tooled for large-scale projects" and better security features.[4] Google works on Dart to help it build more complex, full-featured client-side Web applications.[5]

Dart is a class-based, single inheritance, object-oriented language with C-style syntax. It supports interfaces, abstract classes, reified generics, and optional typing. Static type annotations do not affect the runtime semantics of the code. Instead, the type annotations can provide documentation for tools like static checkers and dynamic run time checks.

The project was founded by Lars Bak and Kasper Lund.

Usage

There are three primary ways to run Dart code:

Compiled as Javascript: When running Dart code in a web browser, the primary intended mechanism is to pre-compile the Dart code into Javascript using the dart2js compiler. Compiled as Javascript, Dart code is compatible with all major browsers with no specific browser adoption of Dart being required. Through optimization of the compiled Javascript output to avoid expensive checks and operations, code written in Dart can, in some cases, run faster than equivalent code hand-written using Javascript idioms.[6]

In the Dartium Browser: The Dart SDK ships with a version of the Chromium web browser modified to include a Dart virtual machine. This browser can run Dart code directly without compilation to Javascript. It is currently not intended for general-purpose use, but rather as a development tool for Dart applications.[7] When embedding Dart code into web apps, the current recommended procedure is to load a bootstrap JavaScript file, "dart.js", which will detect the presence or absence of the Dart VM and load the corresponding Dart or compiled Javascript code, respectively,[8] therefore guaranteeing browser compatibility with or without the custom Dart VM.

Stand-Alone: The Dart SDK also ships with a stand-alone Dart VM, allowing dart code to run in a command-line environment. As the language tools included in the Dart SDK are written primarily in Dart, the stand-alone Dart VM is a critical part of the SDK. These tools include not only the dart2js compiler, but also a package management suite called pub. Dart ships with a complete standard library allowing users to write fully functional system apps, such as custom web servers.[9]

Runtime modes

Dart programs run in one of two modes. In "checked mode", which is not the default mode and must be turned on, dynamic type assertions are enabled. These type assertions can turn on if static types are provided in the code, and can catch some errors when types do not match. For example, if a method is annotated to return a String, but instead returns an integer, the dynamic type assertion will catch this and throw an exception. Running in "checked mode" is recommended for development and testing.

Dart programs run by default in "production mode", which runs with all dynamic type assertions turned off. This is the default mode because it currently is the fastest way to run a Dart program.

Compiler to JavaScript

dartc was the first compiler that emitted JavaScript from Dart code. dartc has been deprecated. Frog was the second instance of a Dart-to-JavaScript compiler, this time written in Dart. Frog never implemented the full semantics of the language, though, and a new compiler called dart2js was created. Also written in Dart, dart2js is the current Dart-to-JavaScript compiler and is intended to implement the full Dart language specification and semantics.

On March 28, 2013 the Dart team posted an update on their blog[10] addressing Dart code compiled to JavaScript with the dart2js compiler, stating that it now runs faster than handwritten JavaScript on Chrome's V8 JavaScript engine for DeltaBlue benchmark.[11]

Editors

On November 18, 2011, Google released Dart Editor, an open-source Dart editor based on Eclipse components, for Mac OS X, Windows, and Linux-based operating systems.[12] The editor supports syntax highlighting, code completion, JavaScript compilation, running both web and server Dart applications, and debugging.

On August 13, 2012, Google announced the release of an Eclipse plugin for doing Dart development.[13]

JetBrains IDEs also support the Dart language. Dart plugin[14] is available for IntelliJ IDEA, PhpStorm and WebStorm. This plugin supports many features such as syntax highlighting, code completion, refactoring, debugging, and more.

Browser adoption

Dart compiles to JavaScript, allowing Dart applications to run in web browsers. However, there is a special version of Chromium that embeds the Dart virtual machine.[15] This enables this browser to run Dart programs without first being compiled to JavaScript. With the M1 release, the generated JavaScript reaches about 78% of the performance of hand-written JavaScript while native Dart code runs about 21% faster than similar code in V8.[6]

As of May 2013, Microsoft Internet Explorer, Mozilla Firefox, Opera Software's Opera browser, and Apple Safari have no plan to embed a separate Dart VM.[citation needed]

Example

A Hello World example:

main() {
    print('Hello World!');
}

A function to calculate the nth Fibonacci number:

int fib(int n) => (n > 1) ? (fib(n - 1) + fib(n - 2)) : n;
main() {
    print('fib(20) = ${fib(20)}');
}

A simple class:

// Import the math library to get access to the sqrt function.
import 'dart:math' as math;
 
// Create a class for Point.
class Point {
 
    // Final variables cannot be changed once they are assigned.
    // Create two instance variables.
    final num x, y;
 
    // A constructor, with syntactic sugar for setting instance variables.
    Point(this.x, this.y);
 
    // A named constructor with an initializer list.
    Point.origin() : x = 0, y = 0;
 
    // A method.
    num distanceTo(Point other) {
        var dx = x - other.x;
        var dy = y - other.y;
        return math.sqrt(dx * dx + dy * dy);
    }
}
 
// All Dart programs start with main().
main() {
    // Instantiate point objects.
    var p1 = new Point(10, 10);
    var p2 = new Point.origin();
    var distance = p1.distanceTo(p2);
    print(distance);
}

Criticism

Critiques range from attacking the risk of fragmentation and proprietary vendor lock-in (comparable to ActiveX or Flash), to its unique optional type system.

Microsoft's JavaScript team has stated that: "Some examples, like Dart, portend that JavaScript has fundamental flaws and to support these scenarios requires a 'clean break' from JavaScript in both syntax and runtime. We disagree with this point of view."[16] Microsoft later released their own JavaScript superset language, TypeScript. Unlike Dart, Script#, and GWT, TypeScript doesn't discard the syntax of JavaScript, but extends it.[17]

Apple engineer Oliver Hunt, working on the WebKit project (which, at the time, powered both Safari and Google's own Chrome browser) stated:

Adding an additional web facing language (that isn't standardized) doesn't seem beneficial to the project, if anything it seems harmful (cf. VBScript in IE).[18]

[...] Adding direct and exposed support for a non-standard language [Dart] is hostile to the open-web by skipping any form [of] 'consensus' driven language development that might happen, and foisting whatever language we want on the Web instead. This implicitly puts any browser that supports additional proprietary extensions in the same position as a browser supporting something like VBScript, and has the same effect: breaking the open web by making content that only works effectively in a single product.[19]

Mozilla's Brendan Eich, who developed the JavaScript language, stated:[20]

I guarantee you that Apple and Microsoft (and Opera and Mozilla, but the first two are enough) will never embed the Dart VM. So 'Works best in Chrome' and even 'Works only in Chrome' are new norms promulgated intentionally by Google. We see more of this fragmentation every day. As a user of Chrome and Firefox (and Safari), I find it painful to experience, never mind the political bad taste.


See also

References

  1. ^ "Release Notes for Dart's Beta Release". 
  2. ^ "Web Languages and VMs: Fast Code is Always in Fashion. (V8, Dart) - Google I/O 2013". 
  3. ^ "Dart, a new programming language for structured web programming", GOTO conference (presentation) (opening keynote), Århus, 2011‐10‐10. 
  4. ^ a b Miller, Mark S (November 16, 2010). ""Future of Javascript" doc from our internal "JavaScript Summit" last week". [email protected] mailing list. https://gist.github.com/1208618. Retrieved 2012-01-22. Leaked internal Google email.
  5. ^ "Why?", Dart lang (FAQ), "We designed Dart to be easy to write development tools for, well-suited to modern app development, and capable of high-performance implementations." 
  6. ^ a b "JavaScript as a compilation target : Making it fast". Dartlang.org. Retrieved 2013-08-18. 
  7. ^ Dartium | Dart: Structured web apps. Dartlang.org. Retrieved on 2013-07-21.
  8. ^ Embedding Dart in HTML | Dart: Structured web apps. Dartlang.org. Retrieved on 2013-07-21.
  9. ^ An Introduction to the dart:io Library | Dart: Structured web apps. Dartlang.org. Retrieved on 2013-07-21.
  10. ^ Ladd, Seth. (2013-03-28) Dart News & Updates: Why dart2js produces faster JavaScript code from Dart. News.dartlang.org. Retrieved on 2013-07-21.
  11. ^ Dart Performance | Dart: Structured web apps. Dartlang.org. Retrieved on 2013-07-21.
  12. ^ Google Releases Dart Editor for Windows, Mac OS X, and Linux
  13. ^ Google Release Dart Eclipse Plugin
  14. ^ JetBrains Plugin Repository :: Dart. Plugins.intellij.net. Retrieved on 2013-07-21.
  15. ^ Dartium | Dart: Structured web apps. Dartlang.org. Retrieved on 2013-07-21.
  16. ^ Niyogi, Shanku; Silver, Amanda; Montgomery, John; Hoban, Luke; Lucco, Steve (November 22, 2011). "Evolving ECMAScript". IEBlog. Microsoft. Retrieved 2012-01-22. 
  17. ^ "Microsoft TypeScript – the JavaScript we need". Ars tecnica. December 22, 2012. Retrieved 2012-10-22. 
  18. ^ Hunt, Oliver (13:36:26 PST, 5 December 2011). "WebKit branch to support multiple VMs (e.g., Dart)". webkit-dev mailing list. https://lists.webkit.org/pipermail/webkit-dev/2011-December/018787.html. Retrieved 2012-01-22.
  19. ^ Hunt, Oliver (23:24:43 PST, 5 December 2011). "WebKit branch to support multiple VMs (e.g., Dart)". webkit-dev mailing list. https://lists.webkit.org/pipermail/webkit-dev/2011-December/018813.html. Retrieved 2012-01-22.
  20. ^ Eich, Brendan (2011-09-10). ""Even Brendan Eich admitted...". As if I would not expect, nay demand, that Gila...". Hacker News. Y Combinator. Retrieved 2012-01-22. 
  21. ^ Welcome to TypeScript. Typescriptlang.org. Retrieved on 2013-07-21.

Bibliography

External links