<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi all,<div class=""><br class=""></div><div class="">As long as the `for` loop is in the language, I don’t really see the use of `forEach`. I understand that it can read much nicer to write something like `a.map { something }.filter { somethingElse }.forEach {` rather than a `for` loop. However, I think the costs don’t outweigh the benefits. It might look like a for loop can just be replaced by a `forEach`, however, this is not true in the general case. For example, consider the following example:</div><div class=""><br class=""></div><div class=""><div class=""><div class="" style="margin: 0px; font-size: 13px; line-height: normal; font-family: 'Akkurat TT';"> <span class="" style="color: rgb(4, 51, 255);">func</span> indexOf_foreach(element: <span class="" style="color: rgb(52, 149, 175);">Element</span>) -> <span class="" style="color: rgb(52, 149, 175);">Int</span>? {</div><div class="" style="margin: 0px; font-size: 13px; line-height: normal; font-family: 'Akkurat TT';"> <span class="" style="color: rgb(4, 51, 255);">self</span>.<span class="" style="color: rgb(52, 149, 175);">indices</span>.<span class="" style="color: rgb(52, 149, 175);">filter</span> { idx <span class="" style="color: rgb(4, 51, 255);">in</span> <span class="" style="color: rgb(4, 51, 255);">self</span>[idx] <span class="" style="color: rgb(52, 149, 175);">==</span> element }.<span class="" style="color: rgb(52, 149, 175);">forEach</span> { idx <span class="" style="color: rgb(4, 51, 255);">in</span></div><div class="" style="margin: 0px; font-size: 13px; line-height: normal; font-family: 'Akkurat TT';"> <span class="" style="color: rgb(4, 51, 255);">return</span> idx</div><div class="" style="margin: 0px; font-size: 13px; line-height: normal; font-family: 'Akkurat TT';"> }</div><div class="" style="margin: 0px; font-size: 13px; line-height: normal; font-family: 'Akkurat TT';"> <span class="" style="color: rgb(4, 51, 255);">return</span> <span class="" style="color: rgb(4, 51, 255);">nil</span></div><div class="" style="margin: 0px; font-size: 13px; line-height: normal; font-family: 'Akkurat TT';"> }</div></div></div><div class=""><br class=""></div><div class="">The code above (I realise it’s quite inefficient) might look like it’s returning the first index for which the filter’s condition returned true. However, the first occurrence of return is actually returning inside the closure, not the outer function. So the result of this function is always nil.</div><div class=""><br class=""></div><div class="">Another solution would be to give a good warning/error message here (you’re trying to return an Optional value where we expect a ()). However, this is also problematic when dealing with side-effects. For example:</div><div class=""><br class=""></div><div class="">[1,2,3,4,5].forEach { num in</div><div class=""> print(num)</div><div class=""> if num > 3 { return }</div><div class="">}</div><div class=""><br class=""></div><div class="">I think it’s quite easy to get things wrong with forEach, so I’d propose removing it and rather having a regular for loop. (Erica-style).</div><div class=""><br class=""></div><div class="">Chris</div></body></html>