'Bob Nystrom' via Dart Misc
2018-09-12 00:52:47 UTC
Dartisans!
*TL;DR: dartfmt --fix awesomizes your code. Make sure you don't
need Dart 1.x support before running it.*
Now that Dart 2 has shipped, I can tell you about a feature I'm super
excited about. With the move to Dart 2, have a few places where the
language gives you two syntaxes to express the same thing. We want to
minimize the amount of breakage, so the old way is still supported, but
it's good to get onto the latest idioms.
In particular:
1. You can use "=" as the default value separator in named parameters
<https://www.dartlang.org/guides/language/effective-dart/usage#do-use--to-separate-a-named-parameter-from-its-default-value>
like you can for optional positional ones:
// Old:
buyDoughnuts({int howMany: 13}); { ... }
// New:
buyDoughnuts({int howMany = 13}); { ... }
2. The new keyword is no longer needed
<https://www.dartlang.org/guides/language/effective-dart/usage#dont-use-new>
:
// Old:
new Future.value(new Thing(), new OtherThing())
// New:
Future.value(Thing(), OtherThing())
3. The const keyword is no longer needed inside a const context
<https://www.dartlang.org/guides/language/effective-dart/usage#dont-use-const-redundantly>
:
// Old:
const breakfast = {
const Doughnuts(): const [const Cruller(), const BostonCream()],
};
// New:
const breakfast = {
Doughnuts(): [Cruller(), BostonCream()],
};
Because these are purely syntactic changes, we can easily automate them. So
easily, in fact, that dartfmt can do it. If you run:
dartfmt *--fix* -w *<path...>*
Then dartfmt applies the above three transformations and reformats the
result. There are also specific "--fix-" flags if you want to do the three
fixes separately.
The first fix is safe to do. Dart has supported "=" as the named parameter
separator for quite some time.
The latter two fixes are safe if your code no longer needs to support a Dart
1.x runtime or compiler. Optional new and const are only supported in Dart 2.
If you know you are already using optional "new"/"const" then --fix is a
great way to migrate your whole codebase. If you haven't started using them
yet, make sure you can before blasting them all away.
When you do, *make sure to bump the minimum SDK version constraint
<https://www.dartlang.org/tools/pub/pubspec#sdk-constraints> in your
pubspec to 2.0.0*. That way people on older versions of Dart won't
inadvertently try to use your non-backwards-compatible code.
Cheers!
â bob
*TL;DR: dartfmt --fix awesomizes your code. Make sure you don't
need Dart 1.x support before running it.*
Now that Dart 2 has shipped, I can tell you about a feature I'm super
excited about. With the move to Dart 2, have a few places where the
language gives you two syntaxes to express the same thing. We want to
minimize the amount of breakage, so the old way is still supported, but
it's good to get onto the latest idioms.
In particular:
1. You can use "=" as the default value separator in named parameters
<https://www.dartlang.org/guides/language/effective-dart/usage#do-use--to-separate-a-named-parameter-from-its-default-value>
like you can for optional positional ones:
// Old:
buyDoughnuts({int howMany: 13}); { ... }
// New:
buyDoughnuts({int howMany = 13}); { ... }
2. The new keyword is no longer needed
<https://www.dartlang.org/guides/language/effective-dart/usage#dont-use-new>
:
// Old:
new Future.value(new Thing(), new OtherThing())
// New:
Future.value(Thing(), OtherThing())
3. The const keyword is no longer needed inside a const context
<https://www.dartlang.org/guides/language/effective-dart/usage#dont-use-const-redundantly>
:
// Old:
const breakfast = {
const Doughnuts(): const [const Cruller(), const BostonCream()],
};
// New:
const breakfast = {
Doughnuts(): [Cruller(), BostonCream()],
};
Because these are purely syntactic changes, we can easily automate them. So
easily, in fact, that dartfmt can do it. If you run:
dartfmt *--fix* -w *<path...>*
Then dartfmt applies the above three transformations and reformats the
result. There are also specific "--fix-" flags if you want to do the three
fixes separately.
The first fix is safe to do. Dart has supported "=" as the named parameter
separator for quite some time.
The latter two fixes are safe if your code no longer needs to support a Dart
1.x runtime or compiler. Optional new and const are only supported in Dart 2.
If you know you are already using optional "new"/"const" then --fix is a
great way to migrate your whole codebase. If you haven't started using them
yet, make sure you can before blasting them all away.
When you do, *make sure to bump the minimum SDK version constraint
<https://www.dartlang.org/tools/pub/pubspec#sdk-constraints> in your
pubspec to 2.0.0*. That way people on older versions of Dart won't
inadvertently try to use your non-backwards-compatible code.
Cheers!
â bob
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAF8T8LwoNhEuFc%3DiHa9rrUGgGtAawCp0P4WSQg3NCG_Dk8zLXA%40mail.gmail.com.
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAF8T8LwoNhEuFc%3DiHa9rrUGgGtAawCp0P4WSQg3NCG_Dk8zLXA%40mail.gmail.com.