Comment #0 by qs.il.paperinik — 2016-04-01T09:01:39Z
Simple as that, shouldn't this work?
The versions of opApply only differ in the attributes the delegate has.
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
+/
}
Comment #1 by qs.il.paperinik — 2016-04-02T07:37:57Z
(In reply to qs.il.paperinik from comment #0)
> 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;
>
> [ ... ]
>
> /+ (1)
> foreach (string s; x)
> {
> result = s;
> }
> writeln(result); // x.opApply matches more than one declaration
> +/
> /+ (2)
> foreach (string s; x)
> {
> result = s;
> write("");
> }
> writeln(result); // x.opApply matches more than one declaration
> +/
> }
For (1) this is true to some extent. The constructed delegate is pure, but can be matched to the non-pure version of opApply.
For (2) the compiler rejects valid because the delegate is impure and can only match the impure opApply.