Proposal: static ctor priority

Kristian Kilpi kjkilpi at gmail.com
Fri Jun 22 01:18:44 PDT 2007


Something like this is needed. I wouldn't mind doing some manual tweaking  
to get the things compile.
(The actual syntax is not so important for me.)


On Thu, 21 Jun 2007 08:57:32 +0300, Daniel Keep  
<daniel.keep.lists at gmail.com> wrote:
>
> The other night, I was writing code for a game prototype when I ran into
> the old cyclic module dependency problem.
>
> Basically, the issue was that engine imports window, and window needs to
> talk to the engine.  Both have loggers for writing out debug
> information, which are (ideally) created in static ctors.
>
> So, even though there is no *actual* dependency between the ctors, they
> still fail to compile.  Previously, people have suggested flow analysis
> to solve this, but flow analysis would be a lot of work.
>
> Therefore, my proposal is to introduce a new pragma to resolve this.
> Example:
>
> module a;
> import b;
>
> pragma(priority, 2)
> static this()
> {
>     // Do stuff
> }
>
> module b;
> import a;
>
> // defaults to priority 1.
> static this()
> {
>     // Do other stuff
> }
>
> (NB: modules are initialised from higher priority (int.max) to lowest
> (int.min))
>
> This would allow developers to manually break these cycles by specifying
>  the relative order they should be initialised in.  For bonus points,
> make this possible:
>
> module a;
> import b;
>
> pragma(priority, b+1)
> static this() {
> ...
>
> This should be much easier to implement than flow analysis, whilst still
> solving the problem.
>
> Comments?
>
> 	-- Daniel



More information about the Digitalmars-d mailing list