September 09, 2011 Re: How do I create a module-local immutable class object? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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) { }
}
|
September 09, 2011 Re: How do I create a module-local immutable class object? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | 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 |
September 09, 2011 Re: How do I create a module-local immutable class object? | ||||
---|---|---|---|---|
| ||||
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 |
Copyright © 1999-2021 by the D Language Foundation