Following code doesn't work:
----------------------------
import std.stdio;
class Foo
{
void opDispatch(string name)()
{
writeln(name);
}
}
void main()
{
auto foo = new Foo;
with (foo)
{
bar();
}
}
----------------------------
Result:
$ dmd -run main
main.d(16): Error: undefined identifier bar
----------------------------
According to the specifications, WithStatement should be interpreted as follows:
----
with (foo)
{
bar();
}
---- is semantically equivalent to:
Foo tmp = foo;
tmp.bar();
---- is semantically equivalent to:
Foo tmp = foo;
tmp.opDispatch!("bar")();
----
It is incorrect behavior and is a bug. And another thing, this syntax is simply convenient if I could use opDispatch in WithStatement.
Comment #1 by lovelydear — 2012-04-26T09:50:39Z
Not working on 2.059
Comment #2 by yebblies — 2015-02-06T05:17:53Z
*** Issue 9808 has been marked as a duplicate of this issue. ***
Comment #3 by me — 2015-07-18T06:30:19Z
Just ran into this issue. From a cursory look at DMD's source code, I noticed that WithStatement::semantic was adding the symbol's scope before calling body->semantic, which means WithStatement has the same scoping rules as a method.
This means that the problem is that opDispatch doesn't work in symbol scope:
----------------
struct A
{
void opDispatch(string Value)()
{
pragma(msg, Value);
}
void test()
{
// works
this.hello;
// NG
hello;
}
}
----------------
I'm unsure as to whether this is intended behaviour or not, but resolving the opDispatch scope issue would also resolve this issue.
Comment #4 by luis — 2015-09-23T16:11:14Z
I've also ran into this issue...
Comment #5 by joeyemmons — 2015-10-04T08:28:04Z
I have also hit this.
Comment #6 by john.loughran.colvin — 2016-03-04T12:47:38Z
It would be very awesome if this was resolved. Both `opDispatch` and `with` are under-appreciated parts of the language with a lot of potential.
Comment #7 by public2 — 2016-06-10T03:20:58Z
This is likely the same problem as Issue 8000. I add my voice to ask this be resolved.