mapKeysTo<R, M extends KtMutableMap> method Null safety

M mapKeysTo<R, M extends KtMutableMap>(
  1. M destination,
  2. R transform(
    1. KtMapEntry<K, V> entry
    )
)

Populates the given destination map with entries having the keys obtained by applying the transform function to each entry in this Map and the values of this map.

In case if any two entries are mapped to the equal keys, the value of the latter one will overwrite the value associated with the former one.

destination is not type checked by the compiler due to https://github.com/dart-lang/sdk/issues/35518, but will be checked at runtime. M actually is expected to be M extends KtMutableMap<R, V>

Implementation

// TODO Change to `M extends KtMutableMap<R, V>` once https://github.com/dart-lang/sdk/issues/35518 has been fixed
M mapKeysTo<R, M extends KtMutableMap<dynamic, dynamic>>(
    M destination, R Function(KtMapEntry<K, V> entry) transform) {
  assert(() {
    if (destination is! KtMutableMap<R, V> && mutableMapFrom<R, V>() is! M) {
      throw ArgumentError("mapKeysTo destination has wrong type parameters."
          "\nExpected: KtMutableMap<$R, $V>, Actual: ${destination.runtimeType}"
          "\nEntries after key transformation with $transform have type KtMapEntry<$R, $V> "
          "and can't be copied into destination of type ${destination.runtimeType}."
          "\n\n$kBug35518GenericTypeError");
    }
    return true;
  }());
  for (final element in iter) {
    destination.put(transform(element), element.value);
  }
  return destination;
}