Simple as that, shouldn't this work?
struct X
{
int opApply(int delegate(string) dg)
{
return dg("impure");
}
int opApply(int delegate(string) pure dg) pure
{
return dg("pure");
}
}
void main()
{
X x;
string result;
x.opApply(
(string s)
{
result = s;
return 0;
}
);
writeln(result); // does compile and prints "pure".
x.opApply(
(string s)
{
result = s;
write("");
return 0;
}
);
writeln(result); // does compile and prints "impure".
/+ (1)
foreach (string s; x)
{
result = s;
}
writeln(result); // does not compile: x.opApply matches more
than one declaration
+/
/+ (2)
foreach (string s; x)
{
result = s;
write("");
}
writeln(result); // does not compile: x.opApply matches more
than one declaration
+/
}
Can someone explain me, why the compiler cannot resolve the right
opApply? From what I know, it generates a delegate with inferred
pure attribute and looks for the best match of opApply. Why does
the manual rewrite work? If I've done the rewrite improperly,
please tell me.