filterTo<C extends KtMutableCollection> method

C filterTo<C extends KtMutableCollection>(
  1. C destination,
  2. bool predicate(
    1. T
    )
)

Appends all elements matching the given predicate to the given destination.

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

Implementation

// TODO Change to `C extends KtMutableCollection<T>` once https://github.com/dart-lang/sdk/issues/35518 has been fixed
C filterTo<C extends KtMutableCollection<dynamic>>(
    C destination, bool Function(T) predicate) {
  assert(() {
    if (destination is! KtMutableCollection<T> && mutableListOf<T>() is! C) {
      throw ArgumentError("filterTo destination has wrong type parameters."
          "\nExpected: KtMutableCollection<$T>, 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.add(element);
    }
  }
  return destination;
}