Cyclic depency with class static ctor
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Thu Oct 26 15:38:01 PDT 2006
Frank Benoit (keinfarbton) wrote:
> I have project with many classes which also sometimes have static ctors.
> Now I get the runtime error "cyclic dependency in module ...".
>
> Is that a bug in the d runtime system, or is it just not documented,
> that this runtime error is also possible for static ctors?
http://www.digitalmars.com/d/module.html#staticorder (under "Order of
Static Construction")
-----
Cycles (circular dependencies) in the import declarations are allowed as
long as not both of the modules contain static constructors or static
destructors. Violation of this rule will result in a runtime exception.
-----
So it's definitely documented. Not as a bug, but as a limitation.
The reason for this is probably so that static constructors can assume
imported modules have been properly initialized.
> Now I have these 300+ ported classes, in 300+ modules. And they import
> each other and many of them have a static ctor. What can I do? How can I
> resolve the problems?
>
> I can think of the possibility to replace all "static this()" with a
> "public static void static_this()" and make a global module where i call
> all these ctor in a manual defined order. Is that the way to go?
If you want to go this way, remember you don't have to do this for *all*
modules, just the ones involved in a cyclic dependency whose static
constructors don't depend on the other ones having been run yet. Then
call it from the start of the static constructors of any modules
importing it. If there's multiple such modules, make sure it doesn't
matter if it's run multiple times, since you can't rely on any module
being initialized before another (unless it's imported by it).
More information about the Digitalmars-d
mailing list