Bug 16255 – std.algorithm.iteration.each on opApply doesn't support ref

Status
REOPENED
Severity
normal
Priority
P3
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-07-09T01:04:49Z
Last change time
2024-12-01T16:27:26Z
Assigned to
No Owner
Creator
Steven Schveighoffer
Moved to GitHub: phobos#10189 →

Comments

Comment #0 by schveiguy — 2016-07-09T01:04:49Z
Look at this code: struct S { int x; int opApply(int delegate(ref int _x) dg) { return dg(x); } } void main() { S s; foreach(ref a; s) ++a; assert(s.x == 1); } If we replace the foreach loop with an each call, we would do: s.each!"++a"; Except it doesn't work. When each accepts the iterable, it accepts it by value. It should accept it by reference (if it's a class, it could take the class reference by value). Otherwise, the point of using ref throughout each is kind of useless.
Comment #1 by jrdemail2000-dlang — 2016-10-09T21:25:24Z
An example that appears related: void main() { import std.algorithm : each; int[] dynamicArray = [1, 2, 3, 4, 5]; int[5] staticArray = [1, 2, 3, 4, 5]; dynamicArray.each!((ref x) => x++); assert(dynamicArray == [2, 3, 4, 5, 6]); // modified staticArray.each!((ref x) => x++); assert(staticArray == [1, 2, 3, 4, 5]); // not modified staticArray[].each!((ref x) => x++); assert(staticArray == [2, 3, 4, 5, 6]); // modified } Using a ref parameter works in the range cases, but not the static array case (opApply).
Comment #2 by greeenify — 2016-12-23T02:04:46Z
> Except it doesn't work. When each accepts the iterable, it accepts it by value. It should accept it by reference (if it's a class, it could take the class reference by value). Otherwise, the point of using ref throughout each is kind of useless. A simple solution with `auto ref` for Iterables: https://github.com/dlang/phobos/pull/4991
Comment #3 by github-bugzilla — 2016-12-24T20:38:38Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/f903de7bc6a507e787323d633679872c5c8e618c Fix issue 16255 - std.algorithm.iteration.each on opApply doesn't support ref https://github.com/dlang/phobos/commit/515b6a4ea2d9485a01efbf212357b623980bfa82 Merge pull request #4991 from wilzbach/fix-issue-16255-each-opApply Fix issue 16255 - std.algorithm.iteration.each on opApply doesn't support
Comment #4 by github-bugzilla — 2017-01-07T03:03:18Z
Commits pushed to stable at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/f903de7bc6a507e787323d633679872c5c8e618c Fix issue 16255 - std.algorithm.iteration.each on opApply doesn't support ref https://github.com/dlang/phobos/commit/515b6a4ea2d9485a01efbf212357b623980bfa82 Merge pull request #4991 from wilzbach/fix-issue-16255-each-opApply
Comment #5 by github-bugzilla — 2017-01-16T23:26:09Z
Commits pushed to newCTFE at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/f903de7bc6a507e787323d633679872c5c8e618c Fix issue 16255 - std.algorithm.iteration.each on opApply doesn't support ref https://github.com/dlang/phobos/commit/515b6a4ea2d9485a01efbf212357b623980bfa82 Merge pull request #4991 from wilzbach/fix-issue-16255-each-opApply
Comment #6 by b2.temp — 2017-02-05T22:16:10Z
Is this supposed to work now ? void main() { int[][] arr = void; arr.length = 10; version(all) { import std.algorithm.iteration : each; arr.each!((ref a){a.length = 10; a[] = -1;}); } else { foreach(ref a; arr) { a.length = 10; a[] = -1; } } }
Comment #7 by acehreli — 2022-06-10T20:12:00Z
(In reply to Basile-z from comment #6) > Is this supposed to work now ? > > > void main() > { > int[][] arr = void; That is the problem. We can't use arr in any meaningful way because it already has garbage number of garbage elements. > arr.length = 10; It reduces the length to 10 but all the elements are garbage.
Comment #8 by robert.schadek — 2024-12-01T16:27:26Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10189 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB