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, but will be checked at runtime. M actually is expected to be M extends KtMutableMap<K, R>


// TODO Change to `M extends KtMutableMap<K, R>` once 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}."
    return true;
  for (final element in iter) {
    destination.put(element.key, transform(element));
  return destination;