mapValuesTo<R, M extends KtMutableMap> method Null safety

M mapValuesTo<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 of this map and the values obtained by applying the transform function to each entry in this Map.

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<K, R>

Implementation

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