Static constructors inconsistency

spec via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 26 12:10:04 PDT 2014


Hello, i'am experiencing some non consistent behavior with static 
constructors. I wonder if this is some bug or i just don't know 
enough of D (most probably!).. Code example bellow:

//------------------------------------------
module main;

import std.stdio;

class B
{
	static this()
	{
		A a = new A;
		int val = a.getValue("A");
	}
}

class A {
	private static int[string] aa;
	
	static this()
	{
		aa = ["A":1, "B":2];
		writeln("@ A static ctor!");
	}

	int getValue(string str)
	{ 	
		return aa[str];	
	}
}



void main(string[] args)
{
	B B = new B;
}
//------------------------------------------

The text "@ A static ctor!" never gets printed. The inconsistency 
(IMHO) is that if i do any of this:
- Declare class A before B;
- Change the var declared in A from an AA to some other p.e int;
- Comment the line "int val = a.getValue("A")";

Maybe there are some other points, but at this point i got 
confused and decided trying to understand it. Anyway with any of 
those changes the text gets printed.

Anyone able to shed some light into this?

Cheers,


More information about the Digitalmars-d-learn mailing list