commit | a19ae4ee688e5297ef95970eb1077cba3fddb5eb | [log] [tgz] |
---|---|---|
author | Paul Berry <paulberry@google.com> | Tue Nov 21 19:14:49 2023 +0000 |
committer | Commit Queue <dart-scoped@luci-project-accounts.iam.gserviceaccount.com> | Tue Nov 21 19:14:49 2023 +0000 |
tree | 8994432b64c0e10a37f9d63669eb27dbfeb5e44a | |
parent | 66fd5972bdff6c113330c9b3f1e7216fb7dd61b5 [diff] |
Enable the extension types language feature. This language feature allows the user to declare a static type using `extension type` syntax, for example: extension type IdNumber(int i) { operator <(IdNumber other) => i < other.i; bool isValid(Some parameters) => ...; } This behaves similarly to a "wrapper" class: class IdNumber { final int i; IdNumber(this.i); operator <(IdNumber other) => i < other.i; bool isValid(Some parameters) => ...; } However, at runtime, no wrapper objects are created; instead, an instance of the extension type is represented directly by its "representation type" (`int` in the above example), and methods like `isValid` are resolved statically. This gives developers an abstraction mechanism with the advantage of zero runtime performance cost, since no extra heap space is required, and no extra instructions are needed to convert between an extension type and its underlying representation. The disadvantage of using extension types as an abstraction mechanism is that since no wrapper objects are created at runtime, the abstraction can be bypassed using `dynamic`, runtime casts, or by "laundering" the object through contravariant generic methods like `List.add` (which are runtime checked in Dart). For example: main() { var id = IdNumber(1); var list1 = <int>[]; List<Object> list2 = list1; list2.add(id); // OK at compile time because `IdNumber` is a // subtype of `Object`. OK at runtime because // at runtime, `IdNumber` and `int` are // indistinguishable. int i = list1[0]; print(i); } Extension types are expected to be particularly useful for low-overhead decoding of external data formats (such as JSON), and for inter-operation with other languages (such as Javascript). For additional information see the feature specification: https://github.com/dart-lang/language/blob/main/accepted/future-releases/extension-types/feature-specification.md Change-Id: I900a3a25dcfc38bfa9c9f9b5b9fa20f362883653 Tested: Standard trybots Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335062 Reviewed-by: Alexander Markov <alexmarkov@google.com> Reviewed-by: Leaf Petersen <leafp@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
Dart is:
Optimized for UI: Develop with a programming language specialized around the needs of user interface creation.
Productive: Make changes iteratively: use hot reload to see the result instantly in your running app.
Fast on all platforms: Compile to ARM & x64 machine code for mobile, desktop, and backend. Or compile to JavaScript for the web.
Dart's flexible compiler technology lets you run Dart code in different ways, depending on your target platform and goals:
Dart Native: For programs targeting devices (mobile, desktop, server, and more), Dart Native includes both a Dart VM with JIT (just-in-time) compilation and an AOT (ahead-of-time) compiler for producing machine code.
Dart Web: For programs targeting the web, Dart Web includes both a development time compiler (dartdevc) and a production time compiler (dart2js).
Dart is free and open source.
See LICENSE and PATENT_GRANT.
Visit dart.dev to learn more about the language, tools, and to find codelabs.
Browse pub.dev for more packages and libraries contributed by the community and the Dart team.
Our API reference documentation is published at api.dart.dev, based on the stable release. (We also publish docs from our beta and dev channels, as well as from the primary development branch).
If you want to build Dart yourself, here is a guide to getting the source, preparing your machine to build the SDK, and building.
There are more documents on our wiki.
The easiest way to contribute to Dart is to file issues.
You can also contribute patches, as described in Contributing.