[Issue 18866] Overload from opDispatch ignored in WithStatement
https://issues.dlang.org/show_bug.cgi?id=18866 Paul Backus changed: What|Removed |Added CC||snarwin+bugzi...@gmail.com --- Comment #8 from Paul Backus --- Another example, from the forums: --- enum Suit { clubs, spades, hearts, diamonds } struct Card { void opDispatch(string s)(.Suit) {} } void main() { Card c; with (c) Suit = .Suit.diamonds; // Error: `Suit` is not an lvalue and cannot be modified } --- It doesn't seem to matter whether the existing symbol is a function, a type, a variable, or anything else; or whether it's declared at module scope or locally. As long as any symbol with the requested name exists in any enclosing scope, the with statement will not call opDispatch. --
[Issue 18866] Overload from opDispatch ignored in WithStatement
https://issues.dlang.org/show_bug.cgi?id=18866 Iain Buclaw changed: What|Removed |Added Priority|P1 |P3 --
[Issue 18866] Overload from opDispatch ignored in WithStatement
https://issues.dlang.org/show_bug.cgi?id=18866 Mike Franklin changed: What|Removed |Added See Also||https://issues.dlang.org/sh ||ow_bug.cgi?id=19588 --
[Issue 18866] Overload from opDispatch ignored in WithStatement
https://issues.dlang.org/show_bug.cgi?id=18866 --- Comment #7 from John Hall --- I get that. The point I wanted to highlight was that even if it's not changed at least the spec can be beefed up. --
[Issue 18866] Overload from opDispatch ignored in WithStatement
https://issues.dlang.org/show_bug.cgi?id=18866 --- Comment #6 from Simen Kjaeraas --- (In reply to John Hall from comment #5) You're right that a new global function will shadow opDispatch, but with the fix the exact opposite problem will appear, so it's not all that simple. --
[Issue 18866] Overload from opDispatch ignored in WithStatement
https://issues.dlang.org/show_bug.cgi?id=18866 John Hall changed: What|Removed |Added CC||john.michael.h...@gmail.com --- Comment #5 from John Hall --- I think a case can be made for fixing this. At a minimum, I think the spec is vague in this instance. On opDispatch it just says that things not found will be forwarded based on opDispatch. On the with statement it makes no reference to opDispatch, suggesting that opDispatch should happen first, rather than last. Moreover, the spec says "This is to reduce the risk of inadvertant breakage of with statements when new members are added to the object declaration." Below is the example from the documentation discussed in one of the PRs mentioned above. The behavior is totally changed if you add in a global function, e.g. void f() { writeln("f global"); } In other words, the way it currently operates raises the risk of inadvertant breakage when new global functions are added. So I think a case can be made for fixing this, or at least making the spec clearer about how with statements interact with opDispatch to make clear how it currently works. --- import std.stdio; struct Foo { void opDispatch(string name)() { mixin("writeln(\"Foo.opDispatch!" ~ name ~ "\");"); } } struct Bar { // `Bar` does not implement `f()` or `opDispatch` } void main() { Foo foo; Bar bar; with(foo) { f(); // prints "Foo.opDispatch!f" with(bar) { f(); // Prior to this Release: Error: undefined identifer `f` // Starting with this release: Prints "Foo.opDispatch!f". // `f`'s resolution is forwarded up the scope hierarchy. } } } --- --
[Issue 18866] Overload from opDispatch ignored in WithStatement
https://issues.dlang.org/show_bug.cgi?id=18866 --- Comment #4 from Simen Kjaeraas--- > there would be absolutely no way of calling fun1() from within the > WithStatement body Sure there would. Assuming the same code as in comment 0, you would call the global fun2 using .fun2();. You can test this by duplicating the line that calls fun2 and adding a period - it will print 'global'. There's no reason to assume that wouldn't work if an overload was available via opDispatch. --
[Issue 18866] Overload from opDispatch ignored in WithStatement
https://issues.dlang.org/show_bug.cgi?id=18866 RazvanNchanged: What|Removed |Added CC||razvan.nitu1...@gmail.com --- Comment #3 from RazvanN --- I'm not sure if this bug report is valid. The current behavior might be a future. If this "bug" would be fixed there would be absolutely no way of calling fun1() from within the WithStatement body and that IMHO is an arbitrary limitation. As things stand now, the compiler first tries to resolve fun1 as a member of S1 and if that's not possible it goes up the enclosing scope. If the chain of scopes is over and fun1 still wasn't resolved then opDispatch is called. In my opinion this makes a lot more sense then calling opDispatch for every method that is not defined in the struct. --
[Issue 18866] Overload from opDispatch ignored in WithStatement
https://issues.dlang.org/show_bug.cgi?id=18866 --- Comment #2 from Mike Franklin--- Or actually it may be this PR that introduced the issue: https://github.com/dlang/dmd/pull/6439 --
[Issue 18866] Overload from opDispatch ignored in WithStatement
https://issues.dlang.org/show_bug.cgi?id=18866 Mike Franklinchanged: What|Removed |Added CC||slavo5...@yahoo.com --- Comment #1 from Mike Franklin --- This may have been introduced by https://github.com/dlang/dmd/pull/7356 --