<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="">Why does that function even compile? For example, the following produces the compiler error: Unexpected non-void return value in void function<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">[<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span>].<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">forEach</span> { x <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> y = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> x</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">Which is what I would expect. But this compiles just fine:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">[<span style="color: rgb(39, 42, 216);" class="">1</span>,<span style="color: rgb(39, 42, 216);" class="">2</span>,<span style="color: rgb(39, 42, 216);" class="">3</span>,<span style="color: rgb(39, 42, 216);" class="">4</span>,<span style="color: rgb(39, 42, 216);" class="">5</span>].<span style="color: rgb(61, 29, 129);" class="">forEach</span> { x <span style="color: rgb(187, 44, 162);" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color: rgb(187, 44, 162);" class="">return</span> x</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Is this just a compiler bug, or am I missing something?</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Tyler</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 8, 2015, at 2:18 PM, André Videla via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Hi, </span><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">As I understand it, the argument for remove forEach is that it’s easy to make confusing code that doesn’t do what it looks like it should do.</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">In this particular example:</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><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 class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">I think the compiler should give us two warnings:</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">- closure returns type Int when it should return type ()</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">- function always return nil (which is a unit type just like () ) when it expects Option<Int></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">In my opinion those warnings should be enough to make the programmer aware that this code doesn’t do what it looks like it does. Typically, returning a type when () is expected shouldn’t go silently.</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">As such I think keeping forEach is important as it allows for every elegant expressions and convey already the already established convention when you use forEach that “I”m only interested in side-effects with elements in this collection”</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">My proposition is then that instead of removing forEach, we should have better type check and warn when the cardinality of the expected type and the returned type don’t match.</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">-André</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class="">On 08 Dec 2015, at 20:07, Chris Eidhof via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">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><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=41N46VivWCZSszJfIBo5Gq4IkbqtRm7tCLAjdnNzXvxi8ViafKS4bfI3ujyuSG3GJ3ldq2EtbzEDjbnI5euvHOHaBWE8kdbzXSNr1rapHVxx9VoRKNZ5mrK9HomYJF60CifKeEN6X7COKdy3jwIYSdcxnZomrV1NNDp-2Bj8O3cn-2BfjhTnb9PlqLhSyuwKNKeCfHvIedQ4K5XhXkRgcU2Hqw-3D-3D" alt="" width="1" height="1" border="0" class="" style="height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;"></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=DOmV4Qxg-2B1XOnX4CfTyH3sCllXs7myBcgXMCpdkqNW26nYm7zdqb7awFJ6tuGBRSarWmtai9I9-2BYqDyD4q5LlOcUEuyUJN7pISdhL1EAu5PGHtQ8Y2gfVoTQ6OZvt9aHnYrj2-2BHZZMXR09XW4-2FEdpYjIZGImptScBAmaLOEzRJPkYyOvQMMYm6nC9mZNNBYw-2Bmmtr-2Bsf0w6tvnd1i03RZrbIJhAJv6lp-2Bqo2h7rheEk-3D" alt="" width="1" height="1" border="0" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><span class="Apple-converted-space"> </span>_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></div></body></html>