Invalid foreach aggregate
Chris via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Nov 17 03:58:20 PST 2015
On Tuesday, 17 November 2015 at 11:26:19 UTC, Marc Schütz wrote:
>
> That really depends on the details, that's why I asked. It
> could be a regression, or it could be that the compiler now
> does stricter checking than before, and your implementation
> wasn't completely correct, or it could be a bug in Phobos if
> you use that to create the range.
>
> If you can post a minimal example that works in 2.067.1, but
> doesn't with the current version, I can try to find the change
> that broke it.
I did just that and I could find the culprit. It's opIndex(). It
works up until 2.068.0, with 2.068.1 I already get this error:
"primitives.d(7): Error: invalid foreach aggregate
doSomething(items).opIndex()"
Here's the example:
=========================
import std.stdio : writeln;
import std.range.primitives;
void main()
{
string[] items = ["a", "b", "c"];
foreach (ref it; items.doSomething())
{
writeln(it);
}
}
auto doSomething(InputRange)(ref InputRange r)
{
struct DoSomething
{
private
{
InputRange r;
size_t cnt;
}
this(InputRange r)
{
this.r = r;
}
@property bool empty()
{
return r.length == 0;
}
@property auto front()
{
return r[0];
}
@property void popFront()
{
r = r[1..$];
}
@property size_t length() const
{
return r.length;
}
@property size_t opIndex()
{
return cnt;
}
@property auto save() const
{
return this;
}
}
return DoSomething(r);
}
More information about the Digitalmars-d-learn
mailing list