I'm checking the implementation of Map. (map.dart in dart:collection)
I find void clear(); in abstract class Map<K, V>. clear() doesn't have any implementation and class Map doesn't extend/implement any other classes. But I can still call clear().
example
Map<int, int> m = <int, int>{1: 1, 2: 2};
m.clear();
Where can I find the implementation of clear()?
CodePudding user response:
My answer is based on the assumption that you want the implementation used when Dart is running natively and not on the web.
The default Map in Dart is a LinkedHashMap. There are several layers before getting the implementation of clear() but I expect this is the one you are looking for:
void clear() {
if (!isEmpty) {
_index = _uninitializedIndex;
_hashMask = _HashBase._UNINITIALIZED_HASH_MASK;
_data = _uninitializedData;
_usedData = 0;
_deletedKeys = 0;
}
}
https://github.com/dart-lang/sdk/blob/2.15.1/sdk/lib/_internal/vm/lib/compact_hash.dart#L333-L341
CodePudding user response:
The {} notation creates a LinkedHashMap and that's where you'll find the implementation. See:
import 'dart:collection';
void main () {
Map<int, int> m = <int, int>{1: 1, 2: 2};
if(m is LinkedHashMap) {
print("It's a LinkedHashMap!");
}
}
Output:
It's a LinkedHashMap!
