@@ -910,7 +910,7 @@ template each(alias pred = "a")
910910 }
911911 }
912912
913- void each (Iterable)(Iterable r)
913+ void each (Iterable)(auto ref Iterable r)
914914 if (isForeachIterable! Iterable)
915915 {
916916 debug (each) pragma (msg, " Using foreach for " , Iterable.stringof);
@@ -928,7 +928,7 @@ template each(alias pred = "a")
928928
929929 // opApply with >2 parameters. count the delegate args.
930930 // only works if it is not templated (otherwise we cannot count the args)
931- void each (Iterable)(Iterable r)
931+ void each (Iterable)(auto ref Iterable r)
932932 if (! isRangeIterable! Iterable && ! isForeachIterable! Iterable &&
933933 __traits (compiles , Parameters! (Parameters! (r.opApply))))
934934 {
@@ -1034,6 +1034,38 @@ template each(alias pred = "a")
10341034 assert (res == [9 , 12 , 15 ]);
10351035}
10361036
1037+ // #16255: `each` on opApply doesn't support ref
1038+ unittest
1039+ {
1040+ int [] dynamicArray = [1 , 2 , 3 , 4 , 5 ];
1041+ int [5 ] staticArray = [1 , 2 , 3 , 4 , 5 ];
1042+
1043+ dynamicArray.each! ((ref x) => x++ );
1044+ assert (dynamicArray == [2 , 3 , 4 , 5 , 6 ]);
1045+
1046+ staticArray.each! ((ref x) => x++ );
1047+ assert (staticArray == [2 , 3 , 4 , 5 , 6 ]);
1048+
1049+ staticArray[].each! ((ref x) => x++ );
1050+ assert (staticArray == [3 , 4 , 5 , 6 , 7 ]);
1051+ }
1052+
1053+ // #16255: `each` on opApply doesn't support ref
1054+ unittest
1055+ {
1056+ struct S
1057+ {
1058+ int x;
1059+ int opApply (int delegate (ref int _x) dg) { return dg(x); }
1060+ }
1061+
1062+ S s;
1063+ foreach (ref a; s) ++ a;
1064+ assert (s.x == 1 );
1065+ s.each! " ++a" ;
1066+ assert (s.x == 2 );
1067+ }
1068+
10371069/**
10381070$(D auto filter(Range)(Range rs) if (isInputRange!(Unqual!Range));)
10391071
0 commit comments