- Pitch: Mutating forEach

While the lack of this feature has been a long-standing pain point, and definitely needs addressing in some way, it has been long enough that we now seem pretty close to the alternative: a for inout loop that lets you mutate its elements.

It's my personal view that adding forEach to the standard library was a major blunder. Its inappropriate use is common in parts of the community. It was originally recommended in the docs only for use at the end of chains, and discouraged in favor of real for loops otherwise, because of the potential confusion with the behavior of return (which acts like continue). At some point though that recommendation was dropped, and besides, was not guidance always seen or followed. I frequently come across cases where I see a forEach instead of a for loop, several lines long, with a return in the middle, and I have no idea if the author intended to return from the outer function or not.

The mutating version would have exactly the same problem, and since it would have more legitimacy, would in turn further encourage use of forEach.

For this reason, I strongly prefer the route of adding a native support for mutating for loops instead. If it were to be another 5 years, that would be one thing, but I think we have a reasonable shot of getting it into the next couple of releases.

24 Likes