D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6400 - opDispatch with WithStatement
Summary: opDispatch with WithStatement
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
: 9808 (view as issue list)
Depends on:
Blocks:
 
Reported: 2011-07-29 08:31 UTC by SHOO
Modified: 2017-12-18 22:57 UTC (History)
9 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description SHOO 2011-07-29 08:31:01 UTC
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 SomeDude 2012-04-26 09:50:39 UTC
Not working on 2.059
Comment 2 yebblies 2015-02-06 05:17:53 UTC
*** Issue 9808 has been marked as a duplicate of this issue. ***
Comment 3 Mithun Hunsur 2015-07-18 06:30:19 UTC
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 Luís Marques 2015-09-23 16:11:14 UTC
I've also ran into this issue...
Comment 5 joeyemmons 2015-10-04 08:28:04 UTC
I have also hit this.
Comment 6 John Colvin 2016-03-04 12:47:38 UTC
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 Jason den Dulk 2016-06-10 03:20:58 UTC
This is likely the same problem as Issue 8000. I add my voice to ask this be resolved.
Comment 8 Citrus 2017-01-13 21:17:52 UTC
Covered by DMD's PR https://github.com/dlang/dmd/pull/6439
Comment 9 github-bugzilla 2017-01-15 09:38:03 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/2c8008781a23f807be89dfaf858814a1308c9f43
Fix issue 6400 - Better interaction between with() and opDispatch

https://github.com/dlang/dmd/commit/c49fb860d507f75556cfb2108986a8b901d15920
Merge pull request #6439 from LemonBoy/b6400

Fix issue 6400 - Better interaction between with() and opDispatch
Comment 10 github-bugzilla 2017-01-16 23:26:39 UTC
Commits pushed to newCTFE at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/2c8008781a23f807be89dfaf858814a1308c9f43
Fix issue 6400 - Better interaction between with() and opDispatch

https://github.com/dlang/dmd/commit/c49fb860d507f75556cfb2108986a8b901d15920
Merge pull request #6439 from LemonBoy/b6400
Comment 11 github-bugzilla 2017-03-22 12:21:13 UTC
Commits pushed to stable at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/2c8008781a23f807be89dfaf858814a1308c9f43
Fix issue 6400 - Better interaction between with() and opDispatch

https://github.com/dlang/dmd/commit/c49fb860d507f75556cfb2108986a8b901d15920
Merge pull request #6439 from LemonBoy/b6400
Comment 12 github-bugzilla 2017-12-06 04:11:47 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/4dfa27ecb16639044961ad995a32352111c19edc
Fix issue 6400 - Better interaction between with() and opDispatch (Part 2)

https://github.com/dlang/dmd/commit/c5defc80b321d8573789279c59161aaa3f3e3654
Merge pull request #7356 from JinShil/fix_6400

Fix issue 6400 - Better interaction between with() and opDispatch (Part 2)
merged-on-behalf-of: Mike Franklin <JinShil@users.noreply.github.com>
Comment 13 github-bugzilla 2017-12-18 22:57:53 UTC
Commits pushed to stable at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/4dfa27ecb16639044961ad995a32352111c19edc
Fix issue 6400 - Better interaction between with() and opDispatch (Part 2)

https://github.com/dlang/dmd/commit/c5defc80b321d8573789279c59161aaa3f3e3654
Merge pull request #7356 from JinShil/fix_6400