www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - can't declare and initialize a static in the same expression

reply Sean Kelly <sean f4.ca> writes:
Shouldn't this be legal?

D:\code\d>type test.d
class A {}

int main()
{
static A a = new A();
return 0;
}

D:\code\d>dmd test.d
test.d(5): non-constant expression new A
Jul 30 2004
parent "Walter" <newshound digitalmars.com> writes:
"Sean Kelly" <sean f4.ca> wrote in message
news:cee7bb$mf$1 digitaldaemon.com...
 Shouldn't this be legal?

 D:\code\d>type test.d
 class A {}

 int main()
 {
 static A a = new A();
 return 0;
 }

 D:\code\d>dmd test.d
 test.d(5): non-constant expression new A

No, all static initializers need to be resolvable at compile time. To do run time initialization, use a static constructor.
Jul 30 2004