Only const or immutable class thread local variables are allowed

Jonathan M Davis jmdavisProg at gmx.com
Mon Dec 9 23:44:06 PST 2013


On Tuesday, December 10, 2013 08:41:25 Joseph Rushton Wakeling wrote:
> On 10/12/13 06:33, Jonathan M Davis wrote:
> > It's still essentially a singleton - it's just that it's a single instance
> > per thread in that case instead of per program. And you avoid all of the
> > threading-related initialization issues with singletons if it's
> > thread-local. Just check whether it's null, initialize it if it is (leave
> > it alone if it isn't), and then do whatever you're going to do with it.
> 
> So for example the below code as an rndGen where Random is a class, not a
> struct?

Yeah. Something like that.

> I think I was misled by the "Only const or immutable ..." part of the error
> message: I'd assumed that any class that actually modified its internal
> state would be disallowed as a static instance.

It's a matter of what you can directly initialize a non-local variable with. 
Module-level variables, static variables, and member variables all have to be 
known at compile time, and you can't have a mutable class instance being 
constructed at compile time and then kept around until runtime.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list