Bug 2671 – Circular imports and static constructors in templates

Status
RESOLVED
Resolution
WORKSFORME
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2009-02-16T08:04:00Z
Last change time
2014-03-01T00:36:10Z
Keywords
rejects-valid
Assigned to
nobody
Creator
samukha

Comments

Comment #0 by samukha — 2009-02-16T08:04:33Z
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.
Comment #1 by mrjnewt — 2009-07-13T15:50:43Z
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.
Comment #2 by bugzilla — 2010-03-10T03:12:13Z
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 -----------------------