Adding empty static this() causes exception
Biotronic via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Sep 12 03:13:59 PDT 2017
On Tuesday, 12 September 2017 at 09:11:20 UTC, Joseph wrote:
> I have two nearly duplicate files I added a static this() to
> initialize some static members of an interface.
>
> On one file when I add an empty static this() it crashes while
> the other one does not.
>
> The exception that happens is
> Cyclic dependency between module A and B.
>
> Why does this occur on an empty static this? Is it being ran
> twice or something? Anyway to fix this?
>
> Seriously, simply adding static this() { } to module B crashes
> the program ;/ module A and module B both import each other
> because there are types that they need to share but that is
> all(one uses an enum of the other and vice versa).
https://dlang.org/spec/module.html#order_of_static_ctor
"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 if you have a static this() in both A and B, and A imports B
and B imports A, you will get this error message. You can pass
--DRT-oncycle=ignore to the program to hide the problem, but a
better solution is to find a way to live with fewer static
this()s.
The reason this exception is thrown is that one module's static
this() might otherwise depend on another module's static this
that depends on the first module again, and there is no good way
to check if it actually depends or just potentially.
--
Biotronic
More information about the Digitalmars-d-learn
mailing list