www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - unittests running before static ctors??

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
I just noticed that unittests are running before static class ctors. Is
this a bug??

I suppose the intention is for unittests to make sure the class
implementation is OK before using it, but this makes it impossible to
use unittests to make sure that static ctors are setting up the initial
states correctly.


T

-- 
No! I'm not in denial!
Jan 26 2012
parent Era Scarecrow <rtcvb32 yahoo.com> writes:
 I just noticed that unittests are running before
static class ctors. Is
 this a bug??
[...]
 I would definitely think that that's a bug. If you're
seeing that happen,
 please report it.
[...] =20 Hmm. It appears that I'm misunderstanding D syntax. What does a "static {...}" block in a class mean? I had this code: =20 =A0=A0=A0 class A { =A0=A0=A0 =A0=A0=A0 static { =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 this() { ... } =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 ... =A0=A0=A0 =A0=A0=A0 } =A0=A0=A0 =A0=A0=A0 unittest { ... } =A0=A0=A0 } =20 It appears that this() is being interpreted as a non-static ctor, which is why it is never run before the unittest (it's never run at all). When I change it to: =20 =A0=A0=A0 class A { =A0=A0=A0 =A0=A0=A0 static this() { ... } =A0=A0=A0 =A0=A0=A0 static { ... } =A0=A0=A0 =A0=A0=A0 unittest { ... } =A0=A0=A0 } =20 Then it works as expected. So I guess I don't quite understand what a static {...} block means.
Static in different locations have different meanings. Compile time logic (= static if), variables, and methods. there's a few other places but i doubt = they have relevance. class C { static this(); //run once during program start up static ~this();//run once during program exit static int x; //on per thread static void func(); //function that doesn't require an object, so call it = like C.func() } void func(){ static int x; //one per thread, but only local to the function static if(1) {} //compile time switch to include following block. } Based on the static {} block, I would THINK it's an access qualifier, mean= ing inside a class or structure 'one per thread'. So doing-- static { int x; float y; void func(); } Should be shorthand for-- static int x; static float y; static void func(); //maybe?? Might think of it kinda like using a with(){} statment. Inside a class it kinda becomes redundant having a staic {this()}. So what= does it do? Probably close to nothing (except init static class vars maybe= ...)=20 If it is suppose to add static to all members and isn't acting correctly, = I would say it would be a bug (although a minor one) since it will think th= e constructor is a static function instead (Named this?) Hmmm gotta ask Wal= ter.
Jan 26 2012