'Samuel Rawlins' via Dart Misc
2018-03-06 18:37:28 UTC
*What is changing?*
As part of the migration of the Dart core libraries to Dart 2, we are
removing the Maps class. No, not the Map class :), the Maps
<https://api.dartlang.org/stable/1.24.3/dart-collection/Maps-class.html> class,
which provided static methods to help developers `implement Map`.
*What will break?*
If you have code which calls a static Maps method, such as Maps.mapToString
<https://api.dartlang.org/stable/1.24.3/dart-collection/Maps/mapToString.html>,
you will need to change this code.
*How do I fix it?*
Typical usage of the Maps class comes as a class which implements Map and
relies on static Maps methods for implementation. Such code should be
changed to `extend MapBase
<https://api.dartlang.org/dev/2.0.0-dev.22.0/dart-collection/MapBase-class.html>`
or extend `with MapMixin
<https://api.dartlang.org/dev/2.0.0-dev.22.0/dart-collection/MapMixin-class.html>`,
which gives you most of the Map methods for free, by just implementing 5
methods that the rest can call upon: keys, operator[], operator[]=, remove
and clear.
*Note:* Extending MapBase or Mixing in MapMixin instead of implementing Map
is a good practice. It future proofs your code against new methods on the
Map class; you won't need to race to add new method implementations in
order to maintain your `implement Map` contract.
Quick example:
```
class MyMap implements Map {
...
V putIfAbsent(String key, V ifAbsent()) {
_mySanityCheck(key);
return Maps.putIfAbsent(this, key, ifAbsent)
}
```
to
```
class MyMap extends MapBase {
...
V putIfAbsent(String key, V ifAbsent()) {
_mySanityCheck(key);
return super.putIfAbsent(key, ifAbsent)
}
```
*Note 2:* Maps.mapToString
<https://api.dartlang.org/stable/1.24.3/dart-collection/Maps/mapToString.html>
is the one static method that Maps provided which is *not* replaced by a
member method of MapBase or MapMixin. This static method has instead been
moved to the MapBase class, still as a static method, in Dart
2.0.0-dev.22.0. Developers need their own replacement (example
<https://github.com/dart-lang/collection/commit/5943e1681204250f33a833eb5550f270357ad6c8>)
or can bump their packages' minimum SDK version to >= 2.0.0-dev.22.0 and
use MapBase.mapToString
<https://api.dartlang.org/dev/2.0.0-dev.22.0/dart-collection/MapBase/mapToString.html>
.
*Why is this change being made?*
The Maps class is a very old hold-over from before Dart featured mixins! As
the class is implemented now, none of the methods use type parameters, and
so none of them play well with the Dart 2 type system (you would be writing
`as` all over, yuck). Rather than upgrade and maintain this redundant
class, we have opted to delete it in favor of the MapMixin implementations.
As part of the migration of the Dart core libraries to Dart 2, we are
removing the Maps class. No, not the Map class :), the Maps
<https://api.dartlang.org/stable/1.24.3/dart-collection/Maps-class.html> class,
which provided static methods to help developers `implement Map`.
*What will break?*
If you have code which calls a static Maps method, such as Maps.mapToString
<https://api.dartlang.org/stable/1.24.3/dart-collection/Maps/mapToString.html>,
you will need to change this code.
*How do I fix it?*
Typical usage of the Maps class comes as a class which implements Map and
relies on static Maps methods for implementation. Such code should be
changed to `extend MapBase
<https://api.dartlang.org/dev/2.0.0-dev.22.0/dart-collection/MapBase-class.html>`
or extend `with MapMixin
<https://api.dartlang.org/dev/2.0.0-dev.22.0/dart-collection/MapMixin-class.html>`,
which gives you most of the Map methods for free, by just implementing 5
methods that the rest can call upon: keys, operator[], operator[]=, remove
and clear.
*Note:* Extending MapBase or Mixing in MapMixin instead of implementing Map
is a good practice. It future proofs your code against new methods on the
Map class; you won't need to race to add new method implementations in
order to maintain your `implement Map` contract.
Quick example:
```
class MyMap implements Map {
...
V putIfAbsent(String key, V ifAbsent()) {
_mySanityCheck(key);
return Maps.putIfAbsent(this, key, ifAbsent)
}
```
to
```
class MyMap extends MapBase {
...
V putIfAbsent(String key, V ifAbsent()) {
_mySanityCheck(key);
return super.putIfAbsent(key, ifAbsent)
}
```
*Note 2:* Maps.mapToString
<https://api.dartlang.org/stable/1.24.3/dart-collection/Maps/mapToString.html>
is the one static method that Maps provided which is *not* replaced by a
member method of MapBase or MapMixin. This static method has instead been
moved to the MapBase class, still as a static method, in Dart
2.0.0-dev.22.0. Developers need their own replacement (example
<https://github.com/dart-lang/collection/commit/5943e1681204250f33a833eb5550f270357ad6c8>)
or can bump their packages' minimum SDK version to >= 2.0.0-dev.22.0 and
use MapBase.mapToString
<https://api.dartlang.org/dev/2.0.0-dev.22.0/dart-collection/MapBase/mapToString.html>
.
*Why is this change being made?*
The Maps class is a very old hold-over from before Dart featured mixins! As
the class is implemented now, none of the methods use type parameters, and
so none of them play well with the Dart 2 type system (you would be writing
`as` all over, yuck). Rather than upgrade and maintain this redundant
class, we have opted to delete it in favor of the MapMixin implementations.
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
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.
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
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.