Cyclic depency with class static ctor

Walter Bright newshound at digitalmars.com
Mon Oct 30 16:59:27 PST 2006


Jarrett Billingsley wrote:
> "Walter Bright" <newshound at digitalmars.com> wrote in message 
> news:ei5kj0$1p2m$2 at digitaldaemon.com...
>> Kristian wrote:
>>> What if some other syntax would be used to define the order of static 
>>> ctors?
>>> For example:
>>>
>>>     import a, b, c;
>>>
>>>     after a: before c: static this();
>> I don't think new syntax is justified because Frank's workaround handles 
>> the problem nicely.
> 
> Except that it makes static ctors entirely useless, since you're just 
> emulating them.  You end up going back to the way you did it in other 
> languages, and static ctors become a useless feature. 

It can appear that way, but there is a crucial point about static 
constructors. They are most useful when you import a module, and that 
module *may* or *may not* have some initialization needs that are hidden 
to you. Static constructors take care of that automatically.

The problem here crops up only when they are cyclic imports, i.e. 
imports that import each other. That means the programmer implementing N 
that imports M is already familiar with the implementation of M, because 
M imports N. Therefore, there is no unknown static constructor nor are 
there any hooks that need to be added to main() by the programmer. 
Furthermore, the programmer can implement the static constructors for M 
and N in such a way that the user of M or N still does not need to add 
any hooks to main(). Essentially, M and N are logically welded into one 
module for the user.

So, static constructors are not useless at all, even when there are 
cycles that have to be dealt with.



More information about the Digitalmars-d mailing list