[Issue 18866] Overload from opDispatch ignored in WithStatement

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Nov 16 15:23:38 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18866

John Hall <john.michael.hall at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |john.michael.hall at gmail.com

--- Comment #5 from John Hall <john.michael.hall at gmail.com> ---
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.
        }
    }
}
---

--


More information about the Digitalmars-d-bugs mailing list