How do I create a module-local immutable class object?
Jonathan M Davis
jmdavisProg at gmx.com
Fri Sep 9 14:42:25 PDT 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.
More information about the Digitalmars-d-learn
mailing list