www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16255] New: std.algorithm.iteration.each on opApply doesn't


          Issue ID: 16255
           Summary: std.algorithm.iteration.each on opApply doesn't
                    support ref
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: schveiguy yahoo.com

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:


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.

Jul 08 2016