Английская Википедия:Dart (programming language)

Материал из Онлайн справочника
Перейти к навигацииПерейти к поиску

Шаблон:Short description Шаблон:About Шаблон:Infobox programming language

Dart is a programming language designed by Lars Bak and Kasper Lund and developed by Google.[1] It can be used to develop web and mobile apps as well as server and desktop applications.

Dart is an object-oriented, class-based, garbage-collected language with C-style syntax.[2] It can compile to machine code, JavaScript, or WebAssembly. It supports interfaces, mixins, abstract classes, reified generics and type inference.[3]

History

Dart was unveiled at the GOTO conference in Aarhus, Denmark, October 10–12, 2011.[4] Lars Bak and Kasper Lund founded the project.[5] Dart 1.0 was released on November 14, 2013.[6]

Dart had a mixed reception at first. Some criticized the Dart initiative for fragmenting the web because of plans to include a Dart VM in Chrome. Those plans were dropped in 2015 with the Dart 1.9 release. Focus changed to compiling Dart code to JavaScript.[7]

Dart 2.0 was released in August 2018 with language changes including a type system.[8]

Dart 2.6 introduced a new extension, dart2native. This extended native compilation to the Linux, macOS, and Windows desktop platforms.[9] Earlier developers could create new tools using only Android or iOS devices. With this extension, developers could deploy a program into self-contained executables. The Dart SDK doesn't need to be installed to run these self-contained executables.[10] The Flutter toolkit integrates Dart, so it can compile on small services like backend support.[11][12]

Dart 3.0 was released in May 2023[13] with changes to the type system to require sound null safety. This release included new features like records, patterns,[14] and class modifiers.[15] Dart 3 also previewed support for Web Assembly.[16]

Specification

Dart released the 5th edition of its language specification on April 9, 2021.[17] This covers all syntax through Dart 2.10. A draft of the 6th edition includes all syntax through 2.13.[18] Accepted proposals for the specification and drafts of potential features can be found in the Dart language repository on GitHub.[19]

ECMA International formed technical committee, TC52,[20] to standardize Dart. ECMA approved the first edition of the Dart language specification as ECMA-408[21] in July 2014 at its 107th General Assembly.[22] Subsequent editions were approved in December 2014,[23] June 2015, and December 2015.[21]

Deploying apps

The Dart software development kit (SDK) ships with a standalone Dart runtime. This allows Dart code to run in a command-line interface environment. The SDK includes tools to compile and package Dart apps.[24] Dart ships with a complete standard library allowing users to write fully working system apps like custom web servers.[25]

Developers can deploy Dart apps in six ways:

Dart deployment methods
Deployment type Target platform Platform-
specific
Requires
Dart VM
Compile
speed
Execution
speed
JavaScript Browser No No Slow Fast
WebAssembly (preview)[26] Browser No No Slow Fast
Self-contained executable macOS, Windows, Linux Yes No Slow Fast
Ahead-of-time module macOS, Windows, Linux Yes No Slow Fast
Just-in-time module macOS, Windows, Linux Yes Yes Fast Slow
Portable module macOS, Windows, Linux No Yes Fast Slow

Deploying to the web

Dart 3 can deploy apps to the web as either JavaScript or WebAssembly apps. WebAssembly support is offered as a preview only Шаблон:As of.

JavaScript

To run in mainstream web browsers, Dart relies on a source-to-source compiler to JavaScript. This makes Dart apps compatible with all major browsers. Dart optimizes the compiled JavaScript output to avoid expensive checks and operations. This results in JavaScript code that can run faster than equivalent code handwritten in pure JavaScript.[27] The first Dart-to-JavaScript compiler was dartc. It was deprecated in Dart 2.0.
The second Dart-to-JavaScript compiler was frog.[28] Written in Dart, it was introduced in 2013 and deprecated in 2020. This should not be confused with Dart Frog,[29] an open-source Dart framework for building backend systems from Very Good Ventures.
The third Dart-to-JavaScript compiler is dart2js. Introduced in Dart 2.0,[30] the Dart-based dart2js evolved from earlier compilers. It intended to implement the full Dart language specification and semantics. Developers use this compiler for production builds. It compiles to minified JavaScript.
The fourth Dart-to-JavaScript compiler is dartdevc.[31] Developers could use this compiler for development builds. It compiles to human-readable JavaScript. On March 28, 2013, the Dart team posted an update on their blog addressing Dart code compiled to JavaScript with the dart2js compiler,[32] stating that it now runs faster than handwritten JavaScript on Chrome's V8 JavaScript engine for the DeltaBlue benchmark.[33]
Prior to Dart 2.18, both dart2js and dartdevc could be called from the command line. Dart 2.18 folded these functions into the Dart SDK. This removed the direct command line wrappers but kept the two compilers. The webdev serve command calls the dartdevc compiler. The webdev build command calls the dart2js compiler.
The Dart SDK compiles to JavaScript in two ways.
To debug code, run webdev serve to compile a larger JavaScript file with human-readable code. Dart-generated JavaScript can be debugged using Chrome only.
$ cd <dart_app_directory>
$ webdev serve [--debug] [-o <target.js>]
To create production apps, run webdev build to compile a minified JavaScript file.
$ cd <dart_app_directory>
$ webdev build [-o <target.js>]

WebAssembly

With the Dart 3 release, Google announced preview support for compiling Dart code to WebAssembly.[26] Full support for Wasm will require adoption of the WasmGC[34] feature into the Wasm standard. Chrome,[35] Firefox,[36] and Safari[37] are integrating WasmGC support.

Deploying to native platforms

Dart can compile to native machine code for macOS, Windows, and Linux as command line tools. Dart can compile apps with user interfaces to the web, iOS, Android, macOS, Windows, and Linux using the Flutter framework.

Self-contained executable

Self-contained executables include native machine code compiled from the specified Dart code file, its dependencies, and a small Dart runtime. The runtime handles type checking and garbage collection. The compiler produces output specific to the architecture on which the developer compiled it. This file can be distributed as any other native executable.
$ dart compile exe <source.dart> -o <target_app>
Generated: <target_app>
$ ./<target_app>

Ahead-of-time module

When compiled ahead of time,[38] Dart code produces portable, performant, and platform-specific modules. It includes all dependent libraries and packages the app needs plus a small Dart runtime. This increases its compilation time. The compiler outputs an app specific to the architecture on which it was compiled.
$ dart compile aot-snapshot <source.dart>
Generated <target_app.aot>
$ dartaotruntime <target_app.aot>

Just-in-time module

When compiled just in time, Dart code produces performant modules that compile fast. This module needs the Dart VM included with the SDK to run. The compiler loads all parsed classes and compiled code into memory the first time the app runs. This speeds up any subsequent run of the app. The compiler outputs an app specific to the architecture on which it was compiled.
$ dart compile jit-snapshot <source.dart>
Compiling <source.dart> to jit-snapshot file <target_app.jit>
Hello world!
$ dart run <target_app.jit>
Hello world!

Dart kernel module

When compiled as a kernel module, Dart code produces a machine-independent format called the Dart Intermediate Representation (Dart IR). The Dart IR bytecode format can work on any architecture that has a Dart VM. This makes this format very portable and quick to compile, but less performant than other compilation outputs.
$ dart compile kernel <source.dart>
Compiling <source.dart> to kernel file <target_app>.dill.
$ dart run <target_app>.dill

Concurrency

To achieve concurrency, Dart uses isolated, independent workers that do not share memory, but use message passing,[39] similarly to Erlang processes (also see actor model). Every Dart program uses at least one isolate, which is the main isolate. Since Dart 2, the Dart web platform no longer supports isolates, and suggests developers use Web Workers instead.[40]

Null safety

Starting with Dart 2.12, Dart introduced sound null safety.[41] This serves as a guarantee that variables cannot return a null value unless it has explicit permission. Null safety prevents the developer from introducing null-pointer exceptions, a common, but difficult to debug, error. With Dart 3, all code must follow sound null safety.

Data storage

Snapshot files, a core part of the Dart VM, store objects and other runtime data.[39]

Script snapshots
Dart programs can be compiled into snapshot files containing all of the program code and dependencies preparsed and ready to execute, allowing fast startups.
Full snapshots
The Dart core libraries can be compiled into a snapshot file that allows fast loading of the libraries. Most standard distributions of the main Dart VM have a prebuilt snapshot for the core libraries that is loaded at runtime.
Object snapshots
Dart uses snapshots to serialize messages that it passes between isolates. As a very asynchronous language, Dart uses isolates for concurrency.[42] An object generates a snapshot, transfers it to another isolate, then the isolate deserializes it.

Editors

On November 18, 2011, Google released Dart Editor, an open-source program based on Eclipse components, for macOS, Windows, and Linux-based operating systems.[43] The editor supports syntax highlighting, code completion, JavaScript compiling, running web and server Dart applications, and debugging.

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

On April 18, 2015, Google retired the Dart Editor in favor of the JetBrains integrated development environment (IDE).[45] Android Studio, IntelliJ IDEA, PyCharm, PhpStorm and WebStorm support a Dart plugin.[46] This plugin supports many features such as syntax highlighting, code completion, analysis, refactoring, debugging, and more. Other editors include plugins for Dart[47] including Sublime Text,[48] Atom,[49] Emacs,[50] Vim[51] and Visual Studio Code.[52]

Chrome Dev Editor

In 2013, the Chromium team began work on an open source, Chrome App-based development environment with a reusable library of GUI widgets, codenamed Spark.[53] The project was later renamed as Chrome Dev Editor.[54] Built in Dart, it contained Spark which is powered by Polymer.[55]

In June 2015, Google transferred the CDE project to GitHub as a free software project and ceased active investment in CDE.[56] The Chrome Dev Editor project was archived on April 24, 2021.[57]

DartPad

To provide an easier way to start using Dart, the Dart team created DartPad at the start of 2015. This online editor allows developers to experiment with Dart application programming interfaces (APIs) and run Dart code. It provides syntax highlighting, code analysis, code completion, documentation, and HTML and CSS editing.[58]

Development tools

The Dart DevTools, written in Dart,[59] include debugging and performance tools.

Flutter

Google introduced Flutter for native app development. Built using Dart, C, C++ and Skia, Flutter is an open-source, multi-platform app UI framework. Prior to Flutter 2.0, developers could only target Android, iOS and the web. Flutter 2.0 released support for macOS, Linux, and Windows as a beta feature.[60] Flutter 2.10 released with production support for Windows[61] and Flutter 3 released production support for all desktop platforms.[62] It provides a framework, widgets, and tools. This framework gives developers a way to build and deploy mobile, desktop, and web apps.[63] Flutter works with Firebase[64] and supports extending the framework through add-ons called packages. These can be found on their package repository, pub.dev.[65] JetBrains also supports a Flutter plugin.[66]

Example

A Hello, World! example:

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

A simple for-loop:[67]

void main() {
  for (var i = 1; i <= 10; i++) {
    print(i);
  }
}

A function to calculate the nth Fibonacci number:

void main() {
  var i = 20;
  print('fibonacci($i) = ${fibonacci(i)}');
}

/// Computes the nth Fibonacci number.
int fibonacci(int n) {
  return n < 2 ? n : (fibonacci(n - 1) + fibonacci(n - 2));
}

A simple class:

// Import the math library to get access to the sqrt function.
// Imported with `math` as name, so accesses need to use `math.` as prefix.
import 'dart:math' as math;

// Create a class for Point.
class Point {

  // Final variables cannot be changed once they are assigned.
  // Declare two instance variables.
  final num x, y;

  // A constructor, with syntactic sugar for setting instance variables.
  // The constructor has two mandatory parameters.
  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 a "getter".
  // Acts the same as a final variable, but is computed on each access.
  num get magnitude => math.sqrt(x * x + y * y);

  // Example of operator overloading
  Point operator +(Point other) => Point(x + other.x, y + other.y);
  // When you instantiate a class such as Point in Dart 2+, new is 
  // an optional word
}

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

Influences from other languages

Dart belongs to the ALGOL language family.[68]Шаблон:Not in ref Its members include C, Java, C#, JavaScript, and others.

The method cascade syntax was adopted from Smalltalk.[69] This syntax provides a shortcut for invoking several methods one after another on the same object.

Dart's mixins were influenced by StrongtalkШаблон:Citation needed[70][71] and Ruby.

Dart makes use of isolates as a concurrency and security unit when structuring applications.[72] The Isolate concept builds upon the Actor model implemented in Erlang.[73]

In 2004, Gilad Bracha (who was a member of the Dart team) and David Ungar first proposed Mirror API for performing controlled and secure reflection in a paper.[74] The concept was first implemented in Self.

See also

Шаблон:Portal

References

Шаблон:Reflist

Bibliography

Шаблон:Refbegin

Шаблон:Refend

External links

Шаблон:Programming languages Шаблон:Google FOSS Шаблон:Google LLC Шаблон:JavaScript Шаблон:Authority control

  1. Шаблон:Cite web
  2. Шаблон:Cite web
  3. Шаблон:Cite web
  4. Шаблон:Citation
  5. Шаблон:Cite web
  6. Шаблон:Cite web
  7. Шаблон:Cite web
  8. Шаблон:Cite web
  9. Шаблон:Cite web
  10. Шаблон:Cite web
  11. Шаблон:Cite web
  12. Шаблон:Cite web
  13. Шаблон:Cite web
  14. Шаблон:Cite web
  15. Шаблон:Cite web
  16. Шаблон:Cite web
  17. Шаблон:Cite web
  18. Шаблон:Cite web
  19. Шаблон:Cite web
  20. Шаблон:Cite web
  21. 21,0 21,1 Шаблон:Cite web
  22. Шаблон:Cite web
  23. Шаблон:Cite web
  24. Шаблон:Cite web
  25. Шаблон:Cite web
  26. 26,0 26,1 Шаблон:Cite web
  27. Шаблон:Cite web
  28. Шаблон:Cite web
  29. Шаблон:Cite web
  30. Шаблон:Cite web
  31. Шаблон:Cite web
  32. Шаблон:Cite web
  33. Шаблон:Cite web
  34. Шаблон:Citation
  35. Шаблон:Cite web
  36. Шаблон:Cite web
  37. Шаблон:Cite web
  38. Шаблон:Cite web
  39. 39,0 39,1 Шаблон:Cite web
  40. Шаблон:Cite web
  41. Шаблон:Cite web
  42. Шаблон:Cite web
  43. Шаблон:Cite web
  44. Шаблон:Cite web
  45. Шаблон:Cite web
  46. Шаблон:Cite web
  47. Шаблон:Cite web
  48. Шаблон:Cite web
  49. Шаблон:Cite web
  50. Шаблон:Citation
  51. Шаблон:Citation
  52. Шаблон:Cite web
  53. Шаблон:Cite web
  54. Шаблон:Cite web
  55. Шаблон:Cite web
  56. Шаблон:Cite web
  57. Шаблон:Cite web
  58. Шаблон:Cite web
  59. Шаблон:Citation
  60. Шаблон:Cite web
  61. Шаблон:Cite web
  62. Шаблон:Cite web
  63. Шаблон:Cite web
  64. Шаблон:Cite web
  65. Шаблон:Cite web
  66. Шаблон:Cite web
  67. Шаблон:Cite web
  68. Шаблон:Cite web
  69. Шаблон:Cite web
  70. Шаблон:Cite journal
  71. Шаблон:Cite web
  72. Шаблон:Cite web
  73. Шаблон:Cite web
  74. Шаблон:Cite journal