Fixing cyclic import static construction problems

Max Samukha maxsamukha at gmail.com
Thu Nov 29 03:52:14 PST 2012


On Thursday, 29 November 2012 at 02:34:11 UTC, Walter Bright 
wrote:
> For discussion:
>
> Cyclical Imports
>
> Problem:
>
> ---- a.d ----
>     module a;
>     import b;
>     static this () { ... }
> ---- b.d ----
>     module b;
>     import a;
>     static this() { ... }
> -------------
>
> Static constructors for a module are only run after static 
> constructors
> for all its imports are run. Circular imports, such as the 
> above, are
> detected at run time and the program is aborted.
>
> This can in general be solved by moving the static 
> constructor(s) into
> a third module, c.d, which does not import a or b. But, people 
> find this
> to be unnatural.

It is natural. It just doesn't cover important use cases.

>
> Proposed Solution:
>
> Add a pragma,
>
>     pragma(cyclic_imports);
>
> This can appear anywhere in a module, and applies globally to 
> that module.
> It means that static constructors from imports that are not 
> part of the cycle
> are run first, and that the static constructor for this module 
> may be run before
> the static constructors of other modules that are part of the 
> cycle.
>
> If any static constructors in such a module with the pragma have
> the @safe attribute, that is a compile time error.

Can we have that implemented in a branch and see how it goes?


More information about the Digitalmars-d mailing list