Only const or immutable class thread local variables are allowed

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Tue Dec 10 00:13:12 PST 2013


On 10/12/13 08:44, Jonathan M Davis wrote:
> Yeah. Something like that.

What I like is that it actually comes out slightly nicer than the existing code 
-- you don't need an extra static boolean to check if the RNG has been 
initialized.  This experience is proving typical of class-based RNG and 
RNG-dependent code so far.

>> 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.

That's obvious in retrospect but not on the basis of the error message:

     Error: variable std.random2.generator.rndGen.result is mutable. Only const 
or immutable class thread local variable are allowed, not 
std.random2.generator.MersenneTwisterEngine!(uint, 32, 624, 397, 31, 
2567483615u, 11, 4294967295u, 7, 2636928640u, 15, 4022730752u, 18, 
1812433253u).MersenneTwisterEngine

Would this be better?

     Cannot initialize thread-local class variable %s with a mutable value.  Only
     const or immutable initial values are allowed (e.g. null).


More information about the Digitalmars-d-learn mailing list