digitalmars.D - Static opDispatch
- Jacob Carlborg (26/26) Oct 01 2011 Have a look at the following code:
- Steven Schveighoffer (25/48) Oct 01 2011 It's an issue with namespace polution. Both instances and the type shar...
- Jacob Carlborg (4/61) Oct 02 2011 This limitation starts to get annoying, but thanks for the workaround.
- Martin Nowak (5/74) Oct 02 2011 Changing this would invert the name lookup priority of opDispatch.
- Jacob Carlborg (6/10) Oct 02 2011 opDispatch should only be called when a piece of code otherwise wouldn't...
Have a look at the following code: class Foo { static Foo instance_; static Foo instance () { if (instance_) return instance_; return instance_ = new Foo; } static auto opDispatch (string name, Args...) (Args args) { mixin("return instance." ~ name ~ "(args);"); } void bar () {} } void main () { Foo.bar; } Compiling the above code results in: Error: need 'this' to access member bar Is it possible somehow to make the above code compile? If not, could the compiler be changed to make the above code compile? -- /Jacob Carlborg
Oct 01 2011
On Sat, 01 Oct 2011 12:34:50 -0400, Jacob Carlborg <doob me.com> wrote:Have a look at the following code: class Foo { static Foo instance_; static Foo instance () { if (instance_) return instance_; return instance_ = new Foo; } static auto opDispatch (string name, Args...) (Args args) { mixin("return instance." ~ name ~ "(args);"); } void bar () {} } void main () { Foo.bar; } Compiling the above code results in: Error: need 'this' to access member bar Is it possible somehow to make the above code compile?It's an issue with namespace polution. Both instances and the type share the same namespace. It's really a bad limitation IMO. I have a bug report that helps try and separate it: http://d.puremagic.com/issues/show_bug.cgi?id=6579 It should work if you use a different class as the singleton: class Foo2 { void bar () {} } class Foo { static Foo2 instance_; static Foo2 instance () { if (instance_) return instance_; return instance_ = new Foo; } static auto opDispatch (string name, Args...) (Args args) { mixin("return instance." ~ name ~ "(args);"); } } -Steve
Oct 01 2011
On 2011-10-02 01:50, Steven Schveighoffer wrote:On Sat, 01 Oct 2011 12:34:50 -0400, Jacob Carlborg <doob me.com> wrote:This limitation starts to get annoying, but thanks for the workaround. -- /Jacob CarlborgHave a look at the following code: class Foo { static Foo instance_; static Foo instance () { if (instance_) return instance_; return instance_ = new Foo; } static auto opDispatch (string name, Args...) (Args args) { mixin("return instance." ~ name ~ "(args);"); } void bar () {} } void main () { Foo.bar; } Compiling the above code results in: Error: need 'this' to access member bar Is it possible somehow to make the above code compile?It's an issue with namespace polution. Both instances and the type share the same namespace. It's really a bad limitation IMO. I have a bug report that helps try and separate it: http://d.puremagic.com/issues/show_bug.cgi?id=6579 It should work if you use a different class as the singleton: class Foo2 { void bar () {} } class Foo { static Foo2 instance_; static Foo2 instance () { if (instance_) return instance_; return instance_ = new Foo; } static auto opDispatch (string name, Args...) (Args args) { mixin("return instance." ~ name ~ "(args);"); } } -Steve
Oct 02 2011
On Sun, 02 Oct 2011 10:38:44 +0200, Jacob Carlborg <doob me.com> wrote:On 2011-10-02 01:50, Steven Schveighoffer wrote:Changing this would invert the name lookup priority of opDispatch. What you request makes Foo.bar inaccessible, i.e. you can't take the address of it any longer. martinOn Sat, 01 Oct 2011 12:34:50 -0400, Jacob Carlborg <doob me.com> wrote:This limitation starts to get annoying, but thanks for the workaround.Have a look at the following code: class Foo { static Foo instance_; static Foo instance () { if (instance_) return instance_; return instance_ = new Foo; } static auto opDispatch (string name, Args...) (Args args) { mixin("return instance." ~ name ~ "(args);"); } void bar () {} } void main () { Foo.bar; } Compiling the above code results in: Error: need 'this' to access member bar Is it possible somehow to make the above code compile?It's an issue with namespace polution. Both instances and the type share the same namespace. It's really a bad limitation IMO. I have a bug report that helps try and separate it: http://d.puremagic.com/issues/show_bug.cgi?id=6579 It should work if you use a different class as the singleton: class Foo2 { void bar () {} } class Foo { static Foo2 instance_; static Foo2 instance () { if (instance_) return instance_; return instance_ = new Foo; } static auto opDispatch (string name, Args...) (Args args) { mixin("return instance." ~ name ~ "(args);"); } } -Steve
Oct 02 2011
On 2011-10-02 17:13, Martin Nowak wrote:Changing this would invert the name lookup priority of opDispatch. What you request makes Foo.bar inaccessible, i.e. you can't take the address of it any longer. martinopDispatch should only be called when a piece of code otherwise wouldn't compile. So in this case, since taking the address of Foo.bar would be legal, opDispatch wouldn't be called. -- /Jacob Carlborg
Oct 02 2011