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"

Reply via email to