Bug 15859 – opApply resolution on attributes

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-04-01T09:01:39Z
Last change time
2018-10-08T21:01:20Z
Keywords
pull, rejects-valid
Assigned to
No Owner
Creator
Bolpat

Comments

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.
Comment #2 by k.hara.pg — 2016-04-02T15:31:02Z
Comment #3 by qs.il.paperinik — 2018-10-08T21:01:20Z
*** This issue has been marked as a duplicate of issue 15624 ***