www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I create a module-local immutable class object?

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
I need to have an object which is initialized only once, so I thought
I could use immutable for that. But I can't do this:

private class Foo {}
immutable Foo foo;

static this()
{
    foo = new Foo;
}

void main() {}

And I can't new the object when it's declared. Even if CTFE could new
objects, it wouldn't be possible because the ctor depends on API
calls.
Sep 09 2011
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Andrej Mitrovic:

 I need to have an object which is initialized only once, so I thought
 I could use immutable for that. But I can't do this:
 
 private class Foo {}
 immutable Foo foo;
 
 static this()
 {
     foo = new Foo;
 }
 
 void main() {}
private class Foo {} immutable Foo foo1; static this() { foo1 = new immutable(Foo); } void main() { auto foo2 = new immutable(Foo); } Bye, bearophile
Sep 09 2011
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/9/11, bearophile <bearophileHUGS lycos.com> wrote:
 private class Foo {}
 immutable Foo foo1;

 static this() {
     foo1 = new immutable(Foo);
 }
Oh right, that's the syntax. Thanks!
Sep 09 2011
prev sibling next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, September 09, 2011 17:37:26 bearophile wrote:
 Andrej Mitrovic:
 I need to have an object which is initialized only once, so I thought
 I could use immutable for that. But I can't do this:
 
 private class Foo {}
 immutable Foo foo;
 
 static this()
 {
 
     foo = new Foo;
 
 }
 
 void main() {}
private class Foo {} immutable Foo foo1; static this() { foo1 = new immutable(Foo); } void main() { auto foo2 = new immutable(Foo); }
But make the constructor shared. Otherwise, it gets initialized once per thread in spite of the fact that immutable is implicitly shared.
Sep 09 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 09/09/2011 11:42 PM, Jonathan M Davis wrote:
 On Friday, September 09, 2011 17:37:26 bearophile wrote:
 Andrej Mitrovic:
 I need to have an object which is initialized only once, so I thought
 I could use immutable for that. But I can't do this:

 private class Foo {}
 immutable Foo foo;

 static this()
 {

      foo = new Foo;

 }

 void main() {}
private class Foo {} immutable Foo foo1; static this() { foo1 = new immutable(Foo); } void main() { auto foo2 = new immutable(Foo); }
But make the constructor shared. Otherwise, it gets initialized once per thread in spite of the fact that immutable is implicitly shared.
Shouldn't the compiler catch this?
Sep 09 2011
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, September 09, 2011 23:44:10 Timon Gehr wrote:
 On 09/09/2011 11:42 PM, Jonathan M Davis wrote:
 On Friday, September 09, 2011 17:37:26 bearophile wrote:
 Andrej Mitrovic:
 I need to have an object which is initialized only once, so I
 thought
 I could use immutable for that. But I can't do this:
 
 private class Foo {}
 immutable Foo foo;
 
 static this()
 {
 
      foo = new Foo;
 
 }
 
 void main() {}
private class Foo {} immutable Foo foo1; static this() { foo1 = new immutable(Foo); } void main() { auto foo2 = new immutable(Foo); }
But make the constructor shared. Otherwise, it gets initialized once per thread in spite of the fact that immutable is implicitly shared.
Shouldn't the compiler catch this?
It should, but it doesn't. http://d.puremagic.com/issues/show_bug.cgi?id=4923 http://d.puremagic.com/issues/show_bug.cgi?id=5207 http://d.puremagic.com/issues/show_bug.cgi?id=6114
Sep 09 2011
prev sibling next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
So much for that idea, immutable breaks property functions. Take a look:

class Foo
{
    this()
    {
        value = true;
    }

     property bool value() { return true; }
     property void value(bool value) { }
}

void main()
{
    auto foo1 = new Foo;
    auto val1 = foo1.value;

    auto foo2 = new immutable(Foo);
    auto val2 = foo2.value;  // fail
}

Is this a known issue or should I file it?
Sep 09 2011
next sibling parent David Nadlinger <see klickverbot.at> writes:
On 9/9/11 11:45 PM, Andrej Mitrovic wrote:
 So much for that idea, immutable breaks property functions. Take a look:

 class Foo
 {
      this()
      {
          value = true;
      }

       property bool value() { return true; }
       property void value(bool value) { }
 }

 void main()
 {
      auto foo1 = new Foo;
      auto val1 = foo1.value;

      auto foo2 = new immutable(Foo);
      auto val2 = foo2.value;  // fail
 }

 Is this a known issue or should I file it?
Did you mark the getter const? David
Sep 09 2011
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 09/09/2011 11:45 PM, Andrej Mitrovic wrote:
 class Foo
 {
      this()
      {
          value = true;
      }

       property bool value() { return true; }
       property void value(bool value) { }
 }
class Foo { this() { value = true; } property bool value() const { return true; } // const property void value(bool value) { } }
Sep 09 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/9/11, Jonathan M Davis <jmdavisProg gmx.com> wrote:
 But make the constructor shared. Otherwise, it gets initialized once per
 thread in spite of the fact that immutable is implicitly shared.
Good call!
Sep 09 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
For crying out loud, shared fails too:

class Foo
{
    shared this()
    {
        value = true;
    }

     property bool value() { return true; }
     property void value(bool value) { }
}

void main()
{
    auto foo1 = new Foo;
    auto val1 = foo1.value;  // fail
}
Sep 09 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, September 09, 2011 23:47:32 Andrej Mitrovic wrote:
 For crying out loud, shared fails too:
 
 class Foo
 {
     shared this()
     {
         value = true;
     }
 
      property bool value() { return true; }
      property void value(bool value) { }
 }
 
 void main()
 {
     auto foo1 = new Foo;
     auto val1 = foo1.value;  // fail
 }
I meant to make the static constructor shared.
Sep 09 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Ok so this is much more involved than I thought. I need to re-read
parts of TDPL again. Sorry for the excessive noise. :p
Sep 09 2011