filterTo<M extends KtMutableMap> method

M filterTo<M extends KtMutableMap>(
  1. M destination,
  2. bool predicate(
    1. KtMapEntry<K, V> entry
    )
)

Appends all entries matching the given predicate into the mutable map given as destination parameter.

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, V>

@return the destination map.

Implementation

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