Hello. Can anyone explain to me, why some simle fu...
# announcements
s
Hello. Can anyone explain to me, why some simle functions on Iterable return list and not Iterable subtype it was applied to? For example:
Copy code
inline fun <T> Iterable<T>.filter(
    predicate: (T) -> Boolean
) : List<T>
If I chain multiple functions, say, using Set, it is simetimes uncomfortable to call
toSet()
each time the list is returned.
l
Try to write one, and you'll see it's impossible as you can't predict how to make a new instance of that subtype.
s
You probably can using reflection
l
You can write one for
Set<T>
though
It cannot work in all cases, and that'd have bad implications like performance by default, especially on Android, and wouldn't work on #C3SGXARS6 or #C0B8L3U69
r
Also, many of them have a
to
alternative (e.g.
mapTo(mutableSetOf()) { ... }
)
s
Brilliant. I somehow missed this feature. Thanks a lot for that