Google Dart adds extension methods


With Dart 2.7 released this week, the Google-developed programming language adds extension methods, allowing developers to add functionality to any type—even to types not under the developer’s control.

Extension methods have the brevity and auto-complete experience of regular method calls. An example provided in a Google bulletin on the Dart 2.7 release illustrates how developers could extend the String class. Developers in the Dart community already have been experimenting with extension methods, including development of a time package using extensions on numcode, the base class for ints and doubles, for the construction of Duration objects.

Extension methods can work with Dart’s type inference. Because extension methods are dispatched dynamically, they cannot be called on values whose type is dynamic. Extension methods are resolved statically, meaning they are as quick as calling a helper method or static function but with a friendlier invocation syntax. Also, extension methods can have type variables and are actually extension members.

Dart 2.7 can be accessed from or in the Flutter 1.12 tool release. Other capabilities in Dart 2.7 include:

  • A characters package, supporting strings viewed as user-perceived characters, also known as Unicode grapheme clusters. The package is available as a technical preview.
  • A null safety static analysis preview. Dart builders previously had announced intent to support null safety, adding support for safely accessing object references without triggering null reference exceptions.

Once perceived as a potential rival to JavaScript, Dart now is positioned as a client-optimized language for building apps on any platform. Dart is billed as being easy to learn for developers who already program in Java, JavaScript, or Swift.

Previous articleGoogle Home vs. Amazon Echo: Battle of the Smart Speakers
Next articleXiaomi Mi Note 10 Review