Static constructor in a template belongs to the module declaring the template, not the module where the template is instantiated (for template instances that are not mixed-in). The following works and that's cool: ---- module a; template Foo(int i) { static this() { } } ---- module b; import a; alias Foo!(1) foo; ---- module c; import a; alias Foo!(1) foo; ---- But if we have two separate instances, the example fails with 'Error: circular initialization dependency with module b' ---- module b; import a; alias Foo!(1) foo; ---- module c; import a; alias Foo!(2) foo; ---- If there is another way to emulate static constructors in circularly imported modules (not by manually calling an initialization function), please share.
I can confirm this as I ran across it while working on a large project. In my case, I use a template mixin to add a static constructor with some special behavior to various classes sprinkled around various modules. The project builds but gives me an error when I try running: ---------------- Error: circular initialization dependency with module epic.window ---------------- This is a pretty major hitch in my project, so I'd love to see this fixed or at least a workaround.
I cannot reproduce this on either D1.057 or D2.041. ------ a.d ------- module a; template Foo(int i) { static this() { } } void main() { } ----- b.d -------- module b; import a; alias Foo!(1) foo; ----- c.d -------- module c; import a; alias Foo!(2) foo; ------------------ dmd -c a dmd -c b dmd -c c dmd a.obj b.obj c.obj a ------ this works too ---- dmd a b c a -----------------------