Dart (programming language)

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Dart
Dart logo wordmark.png
Paradigm Object-oriented and class-based
Designed by Lars Bak and Kasper Lund
Developer Google
First appeared November 14, 2013; 19 months ago (2013-11-14)[1]
Stable release 1.11.1 / July 2, 2015; 23 days ago (2015-07-02)
Typing discipline Optional
License BSD license
Filename extensions .dart
Website www.dartlang.org
Major implementations
Dart VM and Fletch
Influenced by
JavaScript, Smalltalk, Erlang, Strongtalk, and C#[2]

Dart is an open-source Web programming language developed by Google. It was unveiled at the GOTO conference in Aarhus, Denmark, October 10–12, 2011.[3] In order to run in mainstream browsers, Dart relies on a source-to-source compiler to JavaScript. According to the project site, Dart was "designed to be easy to write development tools for, well-suited to modern app development, and capable of high-performance implementations."[4]

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 runtime checks.

History[edit]

The project was founded by Lars Bak and Kasper Lund.[5]

Standardization

Ecma International has formed technical committee TC52[6] to work on standardization of Dart, and, inasmuch as Dart can be compiled to standard JavaScript, it works effectively in any modern browser. Ecma International approved the first edition of Dart language specification at its 107th General Assembly on July 2014.[7] Since then, a second edition has been approved by Ecma.[8]

Usage[edit]

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.[9]
In the Dartium Browser
The Dart SDK ships with a version of the Chromium web browser modified to include a Dart virtual machine (VM). This browser can run Dart code directly without compilation to JavaScript, while the original plan for Chrome to do this was cancelled.[10] It is intended as a development tool for Dart applications, rather than as a general purpose web browser.[11]
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.[12]

Runtime modes[edit]

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 is the fastest way to run a Dart program.

Isolates[edit]

To achieve concurrency, Dart uses isolates, which are independent workers that do not share memory, but instead use message passing. This is similar to Erlang actors. Every Dart program uses at least one isolate, which is the main isolate. When compiled to JavaScript, isolates are transformed into Web Workers.

Snapshots[edit]

Snapshots are a core part of the Dart VM. Snapshots are files which store objects and other runtime data.

Script snapshots

Dart programs can be compiled into snapshot files. These files contain all of the program code and dependencies pre-parsed and ready to execute. This allows super-fast startup times.

Full snapshots

The Dart core libraries can be compiled into a snapshot file which allows super-fast loading of the libraries. Most standard distributions of the main Dart VM have a pre-built snapshot for the core libraries which is loaded at runtime.

Object snapshots

Dart is a very asynchronous language. With this, it uses isolates for concurrency. Since these are workers which pass messages, it needs a way to 'serialize' a message. This is done using a snapshot, which is generated from a given object, and then this is transferred to another isolate for deserialization.

Native mobile apps[edit]

Dart has a virtual machine called Fletch which explores different models of concurrency. Along with this, is a simple API for embedding Dart code in any application. Google is working on full Dart stacks for native mobile app development on both Android and iOS.

Compiling to JavaScript[edit]

dart2js is the current Dart-to-JavaScript compiler from Google, as of 2015, and is written in Dart. dart2js is intended to implement the full Dart language specification and semantics. It is an evolution of earlier compilers: dartc was the first compiler to generate JavaScript from Dart code but has since been deprecated. Frog was the second Dart-to-JavaScript compiler and was written in Dart. Frog never implemented the full semantics of the language, leading to the development of the dart2js compiler.

On March 28, 2013, the Dart team posted an update on their blog[13] 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 the DeltaBlue benchmark.[14]

Editors[edit]

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.[15] 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.[16]

On April 18, 2015, Google announced that the Dart Editor would be retired in favor of the JetBrains IDEs.[17]

JetBrains IDEs are the recommended IDE for the Dart language. The Dart plugin[18] is available for IntelliJ IDEA, PyCharm, PhpStorm and WebStorm. This plugin supports many features such as syntax highlighting, code completion, analysis, refactoring, debugging, and more.

Chrome Dev Editor[edit]

It has been known since November 2013[19] that the Chromium team is working on an open source, Chrome App-based development environment with a reusable library of GUI widgets, codenamed Spark, later renamed as Chrome Dev Editor.[20] It is built in Dart, and contains a GUI widgets library powered by Polymer.[21] Developer preview version is available in Chrome Web Store.

DartPad[edit]

The Dart team created DartPad at the beginning of 2015, to provide a frictionless way to get started with Dart. It is an entirely online editor from which you can experiment with Dart APIs, and run Dart code. It provides syntax highlighting, code analysis, code completion, documentation, and HTML and CSS editing.[22]

SIMD on the web[edit]

In 2013, John McCutchan announced[23] that he had created a performant interface to SIMD instruction sets for the Dart programming language, bringing the benefits of SIMD to web programs for the first time, for users running Google's experimental Dartium browser. The interface consists of two types:

  • Float32×4, 4× single precision floating point values
  • Uint32×4, 4× 32-bit unsigned integer values

Instances of these types are immutable and in optimized code are mapped directly to SIMD registers. Operations expressed in Dart typically are compiled into a single instruction with no overhead. This is similar to C and C++ intrinsics. Benchmarks for 4×4 matrix multiplication, 3D vertex transformation, and Mandelbrot set visualization show near 400% speedup compared to scalar code written in Dart.

Example[edit]

A Hello World example:

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

A function to calculate the nth Fibonacci number:

int fib(int n) => (n > 1) ? (fib(n - 1) + fib(n - 2)) : 1;

void 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);
  }

  // Example of Operator Overloading
  Point operator +(Point other) => new Point(x + other.x, y + other.y);
}

// All Dart programs start with main().
void main() {
  // Instantiate point objects.
  var p1 = new Point(10, 10);
  var p2 = new Point.origin();
  var distance = p1.distanceTo(p2);
  print(distance);
}

Influences from other languages[edit]

Dart's is descendant in the ALGOL language family,[24] alongside C, Java, C#, JavaScript, and others.

The method cascade syntax, which provides a syntactic shortcut for invoking several methods one after another on the same object, is adopted from Smalltalk.

Dart's mixins were influenced by Strongtalk[citation needed][25] and Ruby.

Dart makes use of isolates as a concurrency and security unit when structuring applications.[26] The Isolate concept builds upon the Actor model, which is most famously implemented in Erlang.

The Mirror API for performing controlled and secure reflection was first proposed in a paper[27] by Gilad Bracha (who is a member of the Dart team) and David Ungar and originally implemented in Self.

Criticism[edit]

Dart initially had a mixed reception and the Dart initiative has been criticized by some for fragmenting the web. A lot of this was due to the original plans to include a Dart VM in Chrome. However, these plans were cancelled, in order to focus on compilation to JavaScript.[10]

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."[28] One year later, Microsoft released a JavaScript superset language called TypeScript.[29]

See also[edit]

References[edit]

  1. ^ Ladd, Seth. "Dart 1.0: A stable SDK for structured web apps". Dart News & Updates. Google. Retrieved 15 November 2014. 
  2. ^ "Web Languages and VMs: Fast Code is Always in Fashion. (V8, Dart) - Google I/O 2013". Google. Retrieved 22 December 2013. 
  3. ^ "Dart, a new programming language for structured web programming", GOTO conference (presentation) (opening keynote), Århus conference, 2011-10-10 
  4. ^ "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. 
  5. ^ Ladd, Seth. "What is Dart". What is Dart?. O'REILLY. Retrieved August 16, 2014. 
  6. ^ "TC52 - Dart". Retrieved 2013-12-16. 
  7. ^ Anders Thorhauge Sandholm. "Dart News & Updates". dartlang.org. 
  8. ^ Anders Thorhauge Sandholm. "Dart News & Updates". dartlang.org. 
  9. ^ "JavaScript as a compilation target : Making it fast" (PDF). Dartlang.org. Retrieved 2013-08-18. 
  10. ^ a b Seth Ladd. "Dart News & Updates". dartlang.org. 
  11. ^ "Dartium". Dartlang.org. Retrieved 2013-07-21. 
  12. ^ "An Introduction to the dart:io Library". Dartlang.org. Retrieved 2013-07-21. 
  13. ^ Ladd, Seth (2013-03-28). "Dart News & Updates: Why dart2js produces faster JavaScript code from Dart". News.dartlang.org. Retrieved 2013-07-21. 
  14. ^ "Dart Performance". Dartlang.org. Retrieved 2013-07-21. 
  15. ^ "Google Releases Dart Editor for Windows, Mac OS X, and Linux". 
  16. ^ "Google Release Dart Eclipse Plugin". 
  17. ^ Ladd, Seth (2015-04-30). "The present and future of editors and IDEs for Dart". Dart News & Updates. Google. Retrieved 2015-05-18. 
  18. ^ "JetBrains Plugin Repository : Dart". Plugins.intellij.net. Retrieved 2013-07-21. 
  19. ^ Beaufort, François. "The chromium team is currently actively working". 
  20. ^ – A Chrome app based development environment "GitHub: Spark". 
  21. ^ "Chrome Story: Spark, A Chrome App from Google is an IDE for Your Chromebook". 
  22. ^ Ladd, Seth (2015-05-06). "Announcing DartPad: A friction-free way to explore Dart code". Dart News & Updates. Google. Retrieved 2015-05-18. 
  23. ^ "Bringing SIMD to the web via Dart" (PDF). 
  24. ^ "Algol Family". c2.com. 
  25. ^ Bracha, Gilad; Griswold, David (September 1996). "Extending the Smalltalk Language with Mixins" (PDF). OOPSLA Workshop (OOPSLA). 
  26. ^ "The Essence of Google Dart: Building Applications, Snapshots, Isolates". InfoQ. 
  27. ^ Bracha, Gilad; Ungar, David (2004). "Mirrors: design principles for meta-level facilities of object-oriented programming languages" (PDF). ACM SIGPLAN Notices (ACM) 39 (10): 331–344. doi:10.1145/1035292.1029004. Retrieved 15 February 2014. 
  28. ^ Niyogi, Shanku; Silver, Amanda; Montgomery, John; Hoban, Luke; Lucco, Steve (November 22, 2011). "Evolving ECMAScript". IEBlog. Microsoft. Retrieved 2012-01-22. 
  29. ^ Bright, Peter (2012-10-02). "Microsoft TypeScript: the JavaScript we need, or a solution looking for a problem?". Ars Technica. Condé Nast. Archived from the original on 2015-05-01. Retrieved 2012-10-22. 

Bibliography[edit]

External links[edit]