filterIndexedTo<C extends KtMutableCollection> method

C filterIndexedTo<C extends KtMutableCollection>(
  1. C destination,
  2. bool predicate(
    1. int index,
    2. T
    )
)

Appends all elements matching the given predicate to the given destination. @param predicate function that takes the index of an element and the element itself and returns the result of predicate evaluation on the element.

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 filterIndexedTo<C extends KtMutableCollection<dynamic>>(
    C destination, bool Function(int index, T) predicate) {
  assert(() {
    if (destination is! KtMutableCollection<T> && mutableListOf<T>() is! C) {
      throw ArgumentError(
          "filterIndexedTo 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;
  }());
  var i = 0;
  for (final element in iter) {
    if (predicate(i++, element)) {
      destination.add(element);
    }
  }
  return destination;
}