www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Static opDispatch

reply Jacob Carlborg <doob me.com> writes:
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
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
parent reply Jacob Carlborg <doob me.com> writes:
On 2011-10-02 01:50, Steven Schveighoffer wrote:
 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
This limitation starts to get annoying, but thanks for the workaround. -- /Jacob Carlborg
Oct 02 2011
parent reply "Martin Nowak" <dawg dawgfoto.de> writes:
On Sun, 02 Oct 2011 10:38:44 +0200, Jacob Carlborg <doob me.com> wrote:

 On 2011-10-02 01:50, Steven Schveighoffer wrote:
 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
This limitation starts to get annoying, but thanks for the workaround.
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. martin
Oct 02 2011
parent Jacob Carlborg <doob me.com> writes:
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.

 martin
opDispatch 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