http://d.puremagic.com/issues/show_bug.cgi?id=7220
Summary: Bad initialization when using mixin to generate a static field in a -lib'rary Product: D Version: D2 Platform: x86 OS/Version: Windows Status: NEW Severity: critical Priority: P2 Component: DMD AssignedTo: nob...@puremagic.com ReportedBy: xtzgzo...@gmail.com --- Comment #0 from Alex R�nne Petersen <xtzgzo...@gmail.com> 2012-01-04 10:17:29 PST --- main.d: module main; import std.stdio, faulty; static this() { writefln("Faulty.instance: %s", Faulty.instance); } private int main(string[] args) { writeln("Main"); return 0; } faulty.d: module faulty; import std.stdio; private mixin template DefineCoreType(string type) { mixin("public final class " ~ type ~ "{" ~ " private static " ~ type ~ " _instance;" ~ "" ~ " @property public static " ~ type ~ " instance()" ~ " {" ~ " writeln(\"accessing " ~ type ~ ".instance()\");"~ " return _instance; " ~ " }" ~ "" ~ " static this()" ~ " {" ~ " writeln(\"initializing " ~ type ~"._instance\");"~ " _instance = new " ~ type ~ "();" ~ " }" ~ "" ~ "}"); } mixin DefineCoreType!("Faulty"); Compile with: dmd -lib faulty.d dmd main.d faulty.lib Run the resulting main.exe, and you'll see this output: accessing Faulty.instance() Faulty.instance: null initializing Faulty._instance Main but the expected output is: initializing Faulty._instance accessing Faulty.instance() Faulty.instance: faulty.DefineCoreType!("Faulty").Faulty Main The error does not occur if faulty.d is not compiled as a -lib'rary (i.e. compiled directly into the executable), or if you unroll the mixin so faulty.d becomes like so: module faulty; import std.stdio; // this works properly! public final class Faulty { private static Faulty _instance; @property public static Faulty instance() { writeln("accessing Faulty.instance()"); return _instance; } static this() { writeln("initializing Faulty._instance"); _instance = new Faulty(); } } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------