associateWithTo<V, M extends KtMutableMap> method

M associateWithTo<V, M extends KtMutableMap>(
  1. M destination,
  2. V valueSelector(
    1. T
    )
)

Populates and returns the destination mutable map with key-value pairs for each element of the given collection, where key is the element itself and value is provided by the valueSelector function applied to that key.

If any two elements are equal, the last one overwrites the former value in the 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<T, V>

Implementation

// TODO Change to `M extends KtMutableMap<T, V>` once https://github.com/dart-lang/sdk/issues/35518 has been fixed
M associateWithTo<V, M extends KtMutableMap<dynamic, dynamic>>(
    M destination, V Function(T) valueSelector) {
  assert(() {
    if (destination is! KtMutableMap<T, V> && mutableMapFrom<T, V>() is! M) {
      throw ArgumentError(
          "associateWithTo destination has wrong type parameters."
          "\nExpected: KtMutableMap<$T, $V>, Actual: ${destination.runtimeType}"
          "\ndestination (${destination.runtimeType}) items aren't subtype of "
          "$runtimeType items. Items can't be copied to destination."
          "\n\n$kBug35518GenericTypeError");
    }
    return true;
  }());
  for (final element in iter) {
    destination.put(element, valueSelector(element));
  }
  return destination;
}