forEachApply

Any chance we have a forEachApply (applyForEach?) that I’m overlooking? My searches came up empty. If it does not exist, here’s my vote to add it. This is what I came up with.

public inline fun <T> Iterable<T>.forEachApply(block: T.() -> Unit) {
    for (element in this) element.apply(block)
}

It makes sense for those cases where we need to act within the scope of individual objects inside of a list.

I’d guess that if this doesn’t get many votes, then it is probably not necessary as it’s easy enough to use forEach or roll your own. Aside from that, I [with my trivial knowledge of the overall kotlin language] don’t see any places this would cause problems. I think my example illustrates that the language should be able to add this pretty easily.

Also, It does look prettier to use than forEach under the same circumstances and for the same reasons we use apply… but within the context of iteration.

1 Like

Thanks. I guess I didn’t put the right search query. I’ll use that thread for further discussion.