[MUSIC PLAYING]
FILIP HRACEK: Hi.
I'm Filip from the Flutter team.
Dart is adding a new feature called extension methods.
And you might be asking, What is that,
and how do I use it and, why?
If so, you're watching the right video right now.
But before I continue, I should make something clear.
Extension methods are an advanced convenience feature.
If you're just starting with Dart,
you don't need to know how to create an extension method.
If you don't like the feature you don't ever
need to implement one.
What's the problem that extension methods
are trying to solve?
Imagine you're using someone else's library.
In our example, it's a client library to the NASA Exoplanets
API, which gives us information about planets
outside the solar system.
This library is awesome and accurate.
But for my purposes, it's a little hard to use.
For example, it gives the planet's average temperature
in Kelvin.
But all I really want is Celsius.
It gives us a distance in parsecs.
But all I really want to know is how long
it will take me to get there in my imaginary spaceship.
Now we're developers, so we know what to do.
Make helper functions.
Kelvin to solve this, so we can wrap results from the library
and get what we want.
Check habitable, so we can get a simple, readable condition
statement.
Compute time to reach, so we can get the duration
given a planet and a spaceship.
But that's a lot of wrapping.
I would personally much rather have
these as members of the original class.
We'll get to why I want that in a second.
But now let's focus on why I can't do that.
Remember, the original class is in someone else's library.
So we can't just add those methods on fields.
We could extend the class and add methods there.
But that wouldn't help because the API gives us
the original class not our extended class.
We could create a wrapper class that has
the original class inside it.
But it seems wasteful to wrap an object with another object
just because we want to extend the original class
with some methods.
[MUSIC PLAYING]
You define an extension method by writing extension
on some class, then curly braces,
and then you just pretend you're in the class you're extending.
Now you can use your extensions.
Notice how the code now reads more like spoken English.
Print planet temperature in Celsius.
If planet is habitable, print Yay!
Also note how code completion helps you along the way.
Code completion for extension methods
works just like you'd expect if the methods were
on the original class.
I can write "da" then a few letters
and I get what I want even if I sometimes
forget what it's called.
Additionally, and this is a matter of personal taste,
some people prefer to first get an object
and then do things with it instead
of first coming up with an action
and then giving it an object.
It is not how we talk, at least in English,
but it is how we think, I think.
Now the feature is called extension methods.
But that's mostly because that's how
it's called in other languages.
As we have seen, you can extend classes
with getters and setters, static members, even operators.
It is up to you to decide whether any of these
makes sense in each case.
Because extensions follow the same privacy
rules as everything else in Dart.
You're not making anyone else's life harder.
People first need to import the file with the extension for it
to apply to their code.
That said, with great power comes great responsibility.
If you maintain a Dart library, you
might be tempted to add a method to string that caps [INAUDIBLE]
it to the console.
Or add a new way to do for loops.
Or add 70 new members to every single object.
One of the best things about Dart
is how simple and unsurprising it is.
It closely follows the principle of least astonishment.
This makes it accessible for beginners
but also instantly readable for developers of all experience
levels, and thus, more delightful to work with.
Let's keep that.
That said, let's talk about other crazy things
we can do with extension methods.
You can add functionality to core classes
like int, duration, and string.
Because extension methods understand generics,
you can extend only lists of some type.
Here's an extension that will only
show up on lists of integers.
On the other hand, Dart will not let
you use this extension on a list of strings, for example.
What else?
Extension methods are valuable in source generation.
That's when a tool writes code for you that's
easy but tedious to write.
A classic example is serialization.
You might be using a package like JSON serializable,
or Built_Value.
Those generate serialization code for you.
But these packages still have to ask
you to write glue code so that the generated methods
and classes actually get used.
Well, not anymore.
The source generation package can now
generate the two methods for you using an extension method.
So all you write is the code at the top.
The rest is taken care of by the tool.
Last step.
You can name your extensions.
Just put the name between extension and on.
Extension BetterExoplanet on Exoplanet.
The reason you do that is that now you can control
the scope of the extension.
All the usual Dart semantics work.
You can import hide, you can import show.
And if you name your extension with a starting underscore,
that extension is now private to the file
and you can't see it or use it from elsewhere.
So there you go.
You don't need to ever write extension methods
if you don't want to.
But they can be incredibly useful.
Just remember, with great power comes great responsibility.
Thanks for watching.
And check out dart.dev for more info.
[MUSIC PLAYING]