I am trying to understand this function:
public inline fun <T, R, C : MutableCollection<in R>> Array<out T>.flatMapTo(destination: C, transform: (T) -> Iterable<R>): C {
for (element in this) {
val list = transform(element)
destination.addAll(list)
}
return destination
}
if I swap in with out in MutableCollection<in R>, destination.addAll(list) stops working. The error is that addAll expected nothing.
Why doesn't it work? Clearly, list is an Iterable<R>, not an object of type R.
For example,
val list = listOf("abc")
list.flatMap{it.toList()}
As I see it, Iterable<R> is a list of "abc" and not "abc" itself. I mean the list of "abc" is not R, but the whole Iterable<R>. then why does out stops the code from working?