Dart Extension Methods

Many people criticized Dart because it was missing features like Extensions that programmers knew and love from other programming languages like C#, Swift or Kotlin.

This Year at Google IO, the Dart team announced three new features. One of them is Dart Extension Methods.

Now, a few months later, we can take a look at this feature in the recent release of Dart 2.6.

In this article we will dive into the feature specification, we will talk about the design decision and what else we can do with Dart’s new Extension Methods 💪.

How to enable Dart Extension Methods

You can enable this feature by increasing your Dart version in the pubspec.yaml

and enabling Extension Methods in your analysis_options.yaml

Now, you are able to define Extension Methods like this:

Grammar/Syntax of Dart Extension Methods

You can already tell that the syntax is different from what some people know from Kotlin or Swift. When looking at the language specification we can find a more detailed definition of the grammar.

For those of you who are not familiar, here is a simple explanation:
An Extension starts with the extension keyword followed by an optional name of your Extension. In this example, the name is MyFancyList.
After that, we specify the type that we want to extend using on List<T>.

Why did they decide to do it like this?

Many people proposed to go with a more Kotlin like syntax for Extension Methods. Here is an example:

If we adopt this style to Dart’s function syntax we get something like this:

This already looks really noisy and might take beginners a while to understand what this is doing.

Dart is a language that is easy to learn, everyone that used a C-like language before can instantly start using Dart without going through tons of documentation. This simplicity is what makes not only Dart but Flutter so popular at the moment. When implementing new language features its important to keep this simplicity and choose wisely what keywords or syntax you introduce.

Advantages of this Extension Method Syntax

Instead of introducing the concept of receiver types and increasing the complexity of the syntax for properties, functions, and operators, the Dart team decided to move all Extension Methods or properties into the body of the Extension definition.

That way people only have to learn the new extension keyword and can define new properties, function, and operators in the body like they already did it with classes.

Disadvantages of this Extension Method Syntax

This solution is easier but for some people, this might be too verbose, especially if they just want to extend a type with a simple property.

Another downside is that you are not able to define an Extension in a function/method scope like Kotlin can do it.

Creating Extension Methods

Let’s create an Extension from the example above.

In this case, we omit the Extension name, which makes the Extension file private. If you want to use your Extension Methods in other files you have to name them. Named Extensions can be hidden if they conflict with code that you wrote.

Besides stored properties, an Extension can contain everything a regular class body can contain as well. This makes it powerful and we can use that to create phantom types or to enrich existing APIs with operators.

But you can go even crazier and extend actual function types.

Where this can go

Improving Flutter’s Widget API

You can not talk about Dart features without mentioning the impact this language feature will have on Flutter.

Flutter’s Widget API is designed with composition in mind which means that multiple Widgets can be composed to create UI. Flutter has Widgets for nearly everything, even small things like Padding. The downside of this is that this can result in heavy nesting which makes parts of your code look messy.

SwiftUI is solving this elegantly by using Extensions, and Flutter could do this as well. This Flutter code block could be simplified using Extension Methods: 

Using Extension Methods we can avoid nesting and also chain multiple calls without adding more indentations.

Extensions implementing an interface

This is something that came up a few times during the discussion of Extension Methods but in the current version this is not supported and sadly, according to an issue in the Dart language repo this won’t be added soon.

However, many developers would appreciate this since it’s handy if you want to integrate 3rd party types into your library by making them implement an interface that you defined.

Let’s say you are building a custom map framework and you want to give the user the ability to render Flutter Widgets on your map. For that, you maybe create an interface called MapDrawable.

This feature would allow you to implement this interface on Widget.

Consumers of your API could simply add Widgets to your map.

Even though this feature seems nice, keep in mind: With great power comes great responsibility. This kind of ad-hoc polymorphism can spread additional implementation of a type throughout your project which makes it harder to understand what interfaces it implements.

Conclusion

In the last year, many companies started to use Dart and some developers asked the Dart Team to include some of the language features that they know from Kotlin, C# or Swift.

Extension Methods are only one of all the few new features that we will see in the near future. There are already a few libraries using Extension Methods. One of them is time and there are a few nice prototypes on how to reshape Flutter’s API using Extension Methods (See flutter_swiftui).

It’s great to see more and more people interacting and discussing in the Dart language repository.

Get notified when the next article is born 🐣
(no spam, just one app-development-related article per month)