Comment #3 by lt.infiltrator — 2014-03-18T23:37:10Z
What's the difference between this and using foreach, though?
Comment #4 by dlang-bugzilla — 2014-03-18T23:38:59Z
You can put it at the end of UFCS chains. See e.g. the example included with the pull.
It is essentially a small bit of sugar, but seeing how it's present in other languages, I think it makes sense to add.
Comment #5 by dlang-bugzilla — 2014-03-18T23:39:58Z
Oh, and foreach doesn't have "auto ref", does it? It's one reason to use fun(r.front) + popFront instead of foreach.
Comment #6 by github-bugzilla — 2015-01-11T14:02:57Z
Comment #7 by bearophile_hugs — 2015-01-11T14:54:28Z
Reopened because I think the current design of "each" is too much bug-prone:
void main() {
import std.stdio, std.algorithm;
int[10] a;
a.each!((ref x) => x++);
a.writeln;
a[].each!((ref x, ref i) => x = i);
a.writeln;
a[].each!((ref x, int i) => x = i);
a.writeln;
a[].each!q{ a = i };
a.writeln;
}
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Most Phobos functions don't accept fixed-size arrays. "each" accepts them, but takes them by value...
And "each" accepts functions with a second "i" argument (and offers the index "i" as in the last example), but it ignores it.
This is a mess that will cause many bugs. I suggest to tighten the semantics:
- For uniformity with Phobos it's better to not accept a fixed-size array. If you want "each" to accept them, then take them by reference only.
- How do you use the "i" index with a lambda?
- Wrong functions probably should give a compile-time error.
Comment #8 by github-bugzilla — 2015-02-18T03:40:58Z