Skip to main content

New site for Dart news and articles

For the latest Dart news, visit our new blog at  https://medium.com/dartlang .

Enums and Async primitives in Dart

Support for enums, async, and deferred loading are now officially part of the 2nd revision of the Ecma-408 standard, the Dart Programming Language Specification. The second revision was approved last week at the Ecma General Assembly.


You can find more information about using the new features in our language tour on dartlang.org:

Enums

Enums help developers express clear intent with minimal code, and help tools catch potential bugs. Here is an example of an enumeration of form states:

    enum FormState { OPEN, INPROGRESS, CLOSED }

Tools can even warn developers if they omit an enum value from a switch statement, which helps identify potential bugs.



Async

Dart has always had strong support for asynchronous programming, with core library features such as Future and Stream. Now, with the new language primitives async and await, asynchronous code is much easier to read and write.

The following code is easy to read, because a developer can follow the logic, line by line. However, the lookUpVersion() function actually runs asynchronously!

checkVersion() async {
  var version = await lookUpVersion();
  if (version == expectedVersion) {
    // Do something.
  } else {
    // Do something else.
  }
}

Deferred loading

Users expect web applications to load extremely quickly. Dart's new deferred loading syntax helps a developer send minimal code to the client on the initial download, and load extra functionality on demand. This means that the user downloads only the code that they need to get started, resulting in faster load times.

Enjoy!

We are excited about these new language features and are working hard to provide full-fledged implementations of the new features in the Dart SDK as soon as possible. We look forward to your feedback on the new features.