Are static variables available to other static variables?

XavierAP n3minis-git at yahoo.es
Fri Apr 12 11:26:34 UTC 2019


On Friday, 12 April 2019 at 10:56:32 UTC, Jamie wrote:
> On Friday, 12 April 2019 at 10:49:19 UTC, Jamie wrote:
>> I was trying to declare a static variable dependent on another 
>> static variable, but it didn't work. Are static variables not 
>> known to other static variables at compile time?
>>
>> void main()
>> {	
>> 	C c = new C();
>> }
>>
>> class C
>> {
>>     static size_t A = 2;
>>     static size_t B = 2^^A; // A is not known at compile time
>> }
>
> Ok I'm confused... why does that above not work but this does:
>
> void main()
> {	
>     C c = new C();
> }
> class C
> {
>     static size_t A = 2;
>     static size_t B;
>     static this()
>     {
>         B = 2^^A;
>     }
> }

May this be a bug? Static mutable is a theoretically valid use 
case.

It looks to me that D often optimizes by evaluating at compile 
time. But in this case what is a possible optimization breaks a 
valid program when the optimization is found not to be possible. 
Other languages e.g. C# would by spec run these static 
initializations at run-time during the first use of C.


More information about the Digitalmars-d-learn mailing list