Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 09, 2011 How do I create a module-local immutable class object? | ||||
---|---|---|---|---|
| ||||
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 Re: How do I create a module-local immutable class object? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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 Re: How do I create a module-local immutable class object? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 Re: How do I create a module-local immutable class object? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 Re: How do I create a module-local immutable class object? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 Re: How do I create a module-local immutable class object? | ||||
---|---|---|---|---|
| ||||
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 Re: How do I create a module-local immutable class object? | ||||
---|---|---|---|---|
| ||||
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 Re: How do I create a module-local immutable class object? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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 Re: How do I create a module-local immutable class object? | ||||
---|---|---|---|---|
| ||||
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 Re: How do I create a module-local immutable class object? | ||||
---|---|---|---|---|
| ||||
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.
|
Copyright © 1999-2021 by the D Language Foundation