Jump to page: 1 2
Thread overview
How do I create a module-local immutable class object?
Sep 09, 2011
Andrej Mitrovic
Sep 09, 2011
bearophile
Sep 09, 2011
Andrej Mitrovic
Sep 09, 2011
Jonathan M Davis
Sep 09, 2011
Timon Gehr
Sep 09, 2011
Jonathan M Davis
Sep 09, 2011
Andrej Mitrovic
Sep 09, 2011
David Nadlinger
Sep 09, 2011
Timon Gehr
Sep 09, 2011
Andrej Mitrovic
Sep 09, 2011
Andrej Mitrovic
Sep 09, 2011
Jonathan M Davis
Sep 09, 2011
Andrej Mitrovic
September 09, 2011
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.
September 09, 2011
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
September 09, 2011
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!
September 09, 2011
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.
September 09, 2011
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?



September 09, 2011
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?
September 09, 2011
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!
September 09, 2011
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
September 09, 2011
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
}
September 09, 2011
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.
« First   ‹ Prev
1 2