www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Are static variables available to other static variables?

reply Jamie <notme gmail.com> writes:
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
}
Apr 12 2019
next sibling parent Dennis <dkorpel gmail.com> writes:
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?
Add `const` or `immutable` to A and it will work. I don't know why B can't be initialized with A's initial value if A is mutable, but there's probably a rationale for that.
Apr 12 2019
prev sibling parent reply Jamie <notme gmail.com> writes:
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; } }
Apr 12 2019
next sibling parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
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; } }
A static constructor isn't run at compile time, but upon starting the program. Thus, values that are only available at run time can be used by a static constructor. -- Simen
Apr 12 2019
prev sibling next sibling parent XavierAP <n3minis-git yahoo.es> writes:
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. initializations at run-time during the first use of C.
Apr 12 2019
prev sibling parent XavierAP <n3minis-git yahoo.es> writes:
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; } }
It's not a bug. I finally found it in the spec: https://dlang.org/spec/class.html#static-constructor "All member initializations must be determinable by the compiler at compile time, hence there is no order-of-evaluation dependency for member initializations, and it is not possible to read a value that has not been initialized. Dynamic initialization is performed by a static constructor"
Apr 12 2019