Skip to content

Commit

Permalink
Merge pull request #4991 from wilzbach/fix-issue-16255-each-opApply
Browse files Browse the repository at this point in the history
Fix issue 16255 - std.algorithm.iteration.each on opApply doesn't support
  • Loading branch information
andralex committed Dec 24, 2016
2 parents ee68d84 + f903de7 commit 515b6a4
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions std/algorithm/iteration.d
Expand Up @@ -910,7 +910,7 @@ template each(alias pred = "a")
}
}

void each(Iterable)(Iterable r)
void each(Iterable)(auto ref Iterable r)
if (isForeachIterable!Iterable)
{
debug(each) pragma(msg, "Using foreach for ", Iterable.stringof);
Expand All @@ -928,7 +928,7 @@ template each(alias pred = "a")

// opApply with >2 parameters. count the delegate args.
// only works if it is not templated (otherwise we cannot count the args)
void each(Iterable)(Iterable r)
void each(Iterable)(auto ref Iterable r)
if (!isRangeIterable!Iterable && !isForeachIterable!Iterable &&
__traits(compiles, Parameters!(Parameters!(r.opApply))))
{
Expand Down Expand Up @@ -1034,6 +1034,38 @@ template each(alias pred = "a")
assert(res == [9, 12, 15]);
}

// #16255: `each` on opApply doesn't support ref
unittest
{
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]);

staticArray.each!((ref x) => x++);
assert(staticArray == [2, 3, 4, 5, 6]);

staticArray[].each!((ref x) => x++);
assert(staticArray == [3, 4, 5, 6, 7]);
}

// #16255: `each` on opApply doesn't support ref
unittest
{
struct S
{
int x;
int opApply(int delegate(ref int _x) dg) { return dg(x); }
}

S s;
foreach (ref a; s) ++a;
assert(s.x == 1);
s.each!"++a";
assert(s.x == 2);
}

/**
$(D auto filter(Range)(Range rs) if (isInputRange!(Unqual!Range));)
Expand Down

0 comments on commit 515b6a4

Please sign in to comment.