immutable ctors, immutable members, and TDPL

Steven Schveighoffer schveiguy at yahoo.com
Fri May 24 06:56:05 PDT 2013


On Fri, 24 May 2013 04:47:38 -0400, Dicebot <m.strashun at gmail.com> wrote:

> Also one issue Don has brought my attention to is this snippet:
>
> // -----------------------------
>
> immutable int x = 1;
>
> void main()
> {
> 	import std.stdio;
> 	writeln(x);
> }
>
> static this()
> {
> 	x = 42;
> }
> // (does not compile in 2.062 and won't in 2.063)
>
> // ------------------------------
>
> Whatever approach is taken, I think it should be consistent with  
> structs/classes in a sense that global variables are module members and  
> module constructor is, well, constructor.

This is not what I am looking to fix.  Module ctors are very different  
 from struct ctors.

Also, I specifically avoided the "pre-initialized" values, because that is  
outside the scope of this problem.  We assume that for the sake of this  
argument, the value is uninitialized before the ctor is called.

> But it looks very hard to do because "immutable" is implicitly "shared".  
> Ideas?

Use shared static this, and the runtime should run your module ctors in  
dependency order.  That is, another module that uses x must have this  
module's ctors run before it.

-Steve


More information about the Digitalmars-d mailing list