18075, "e-kayrakli", "What's the litmus test for choosing 'foreach' over 'for'?", "2021-07-16T19:15:56Z"
foreach
specifies order independence in a loop. At least as of today, that's the only semantic difference between a foreach
and a for
.
One potentially confusing case came up in the module code here Remove more order independence pragmas by e-kayrakli · Pull Request #18070 · chapel-lang/chapel · GitHub
There are two ways to think here:
- The loop is over a range with a negative stride, this is a pretty specific ordering and as such we shouldn't use
foreach
- The loop body doesn't have any order-dependence, so the order in which the iterator yields doesn't matter. We should use
foreach
.