Dart 2 Breaking Change: Removing Maps class

190 views
Skip to first unread message

Samuel Rawlins

unread,
Mar 6, 2018, 1:37:35 PM3/6/18
to General Dart Discussion
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 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, 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` or extend `with MapMixin`, 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 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) or can bump their packages' minimum SDK version to >= 2.0.0-dev.22.0 and use MapBase.mapToString.

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.

tatumizer-v0.2

unread,
Mar 6, 2018, 6:04:14 PM3/6/18
to Dart Misc
Once you are at it, maybe you can consider merging 'collection' package into collection library? It has some good stuff in it, which logically belongs to dart:collection.
Also, there's some general purpose collection-related section in quiver. All 3 came from the same dart team, not clear what motivation for keeping these things separate is.
 

Reply all
Reply to author
Forward
0 new messages