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