www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Singletons, alias this and DMD crashing

reply Leandro Motta Barros <lmb stackedboxes.org> writes:
Hello,

I was toying with the idea of adding an 'alias instance this' to a
low-lock singleton, to allow calling MySingleton.method(), instead of
MySingleton.instance.method().

When I tried this, however, DMD 2.062 crashed. Under Linux, after
chewing the code for a long time, it exits with "Error: out of
memory". Under Windows (or rather, dmd.exe under Wine, which is what I
actually tried), it crashes with a "Stack overflow" error.

I reduced offending code to this:

class S
{
   static  property S instance()
   {

         _i = new S();

      return _i;
   }

   static S _i;


}

Commenting out either of the lines marked with "#n" avoids the crash
(but ruins my code, naturally :-P).

Is this a known bug? I didn't found it in the bug tracker, but I
couldn't really think of good keywords to look for.

Cheers,

LMB
May 08 2013
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/08/2013 02:20 PM, Leandro Motta Barros wrote:

 When I tried this, however, DMD 2.062 crashed.
The compiler gets stuck in a loop and allocates infinite memory. I tried it with a not-very-recent git version of dmd: v2.063-devel-f6d55a9-dirty The problem is fixed in that version. Ali
May 08 2013
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2013-05-08 23:20, Leandro Motta Barros wrote:
 Hello,

 I was toying with the idea of adding an 'alias instance this' to a
 low-lock singleton, to allow calling MySingleton.method(), instead of
 MySingleton.instance.method().
This won't work. You need to be able to overload on static, which you can't. I've tried this with opDispatch. http://d.puremagic.com/issues/show_bug.cgi?id=3345 -- /Jacob Carlborg
May 09 2013
parent reply Jacob Carlborg <doob me.com> writes:
On 2013-05-09 11:48, Jacob Carlborg wrote:

 This won't work. You need to be able to overload on static, which you
 can't. I've tried this with opDispatch.
To elaborate. Below is your original with the "foo" method added. class S { static property S instance() { _i = new S(); return _i; } static S _i; void foo () {} } If you try to call it like this: S.foo(), the compiler will complain that "foo" need access to "this". The "alias this" won't kick in. -- /Jacob Carlborg
May 09 2013
parent reply Leandro Motta Barros <lmb stackedboxes.org> writes:
Thanks for the comment! I noticed this wouldn't compile, though I
haven't really wondered why so. My email was more about the compiler
bug than about using alias this with singletons.

Anyway, while trying to circumvent the compiler bug, I did this:

import std.stdio;

class SImpl
{
   void foo()
   {
      writeln("foo!");
   }

   private this() { }
}

class S
{
   static  property SImpl instance()
   {
      if (_i is null)
         _i = new SImpl();

      return _i;
   }

   static SImpl _i;

   alias instance this;
}

void main()
{
   S.foo();
}

This works and compiles with DMD 2.062. (I don't like to have
to write this additional code manually, but I'll try to make something
about it :-) )

LMB



On Thu, May 9, 2013 at 7:04 AM, Jacob Carlborg <doob me.com> wrote:
 On 2013-05-09 11:48, Jacob Carlborg wrote:

 This won't work. You need to be able to overload on static, which you
 can't. I've tried this with opDispatch.
To elaborate. Below is your original with the "foo" method added. class S { static property S instance() { _i = new S(); return _i; } static S _i; void foo () {} } If you try to call it like this: S.foo(), the compiler will complain that "foo" need access to "this". The "alias this" won't kick in. -- /Jacob Carlborg
May 09 2013
parent Jacob Carlborg <doob me.com> writes:
On 2013-05-09 15:07, Leandro Motta Barros wrote:
 Thanks for the comment! I noticed this wouldn't compile, though I
 haven't really wondered why so. My email was more about the compiler
 bug than about using alias this with singletons.

 Anyway, while trying to circumvent the compiler bug, I did this:

 import std.stdio;

 class SImpl
 {
     void foo()
     {
        writeln("foo!");
     }

     private this() { }
 }

 class S
 {
     static  property SImpl instance()
     {
        if (_i is null)
           _i = new SImpl();

        return _i;
     }

     static SImpl _i;

     alias instance this;
 }

 void main()
 {
     S.foo();
 }

 This works and compiles with DMD 2.062. (I don't like to have
 to write this additional code manually, but I'll try to make something
 about it :-) )
Aha, you added an additional class. -- /Jacob Carlborg
May 10 2013