unittests running before static ctors??

Era Scarecrow rtcvb32 at yahoo.com
Thu Jan 26 11:14:47 PST 2012

> > > 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.
> [...]
> Hmm. It appears that I'm misunderstanding D syntax. What
> does a "static
> {...}" block in a class mean? I had this code:
>     class A {
>         static {
> this() { ... }
> ...
>         }
>         unittest { ... }
>     }
> 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:
>     class A {
>         static this() { ... }
>         static { ... }
>         unittest { ... }
>     }
> 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, meaning 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...) 

 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 the constructor is a static function instead (Named this?) Hmmm gotta ask Walter.

More information about the Digitalmars-d-learn mailing list