On 03/22/2011 01:12 AM, Nick Sabalausky wrote:
I'm intending this thread as somewhat of a roundtable-like discussion.
Hopefully we can come up with good material for a short article on Wiki4D,
or maybe the D website, or wherever.

The scenario: A coder is writing some D, compiles, runs and gets a "Cyclic
dependency in static ctors" error. Crap! A pain for experienced D users, and
very bad PR for new D users. (Hopefully we'll eventually get some sort of
solution for this, but in the meantime D users just have to deal with it.)

The question: What now? What strategies do people find useful for dealing
with this? Any specific "first steps" to take? Best practices? Etc.

Aside from the old "start merging modules" PITA that many of us are familiar
with from the old "100 forward reference errors at the drop of a hat" days,
I've found one viable (but still kinda PITA) strategy so far:

1. Look at the line that says: "module foo ->  module bar ->  module foo"

2. Pick one of those two modules (whichever has the simplest static ctors)
and eliminate all the static ctors using the following pattern:

The pattern: The trick is to convert every variable that needs to be
initialized into an "init on first use" ref @property. This "ref @property"
checks a "hasThisBeenInited" bool and, if false, runs all the
initialization. If the variable is a reference type, then *sometimes* you
can get away with just checking for null (though often not, because it will
just get-reinited ).

Example:
------------------------------
// Old:
class Foo { /+...+/ }
int number;
Foo foo;
static this
{
     foo = new Foo();
     number = /+ something maybe involving foo +/;
}

// New:
class Foo { /+...+/ }

private int _number;
@property ref int number()
{
     forceModuleInit();
     return _number;
}

private Foo _foo;
@property ref Foo foo()
{
     forceModuleInit();
     return _foo;
}

bool isModuleInited=false;
static void forceModuleInit() // Hopefully inlined
{
     if(!isModuleInited)
     {
         staticThis();
         isModuleInited = true;
     }
}
static void staticThis() // Workaround for static ctor cycle
{
     foo = new Foo();
     number = /+ something maybe involving foo +/;
}
------------------------------

If one of the variables being inited in the static ctor is something you
*know* will never be accessed before some other specific variable is
accessed, then you can skip converting it to "@property ref" if you want.

It's a big mess, but the conversion can be done deterministically, and even
mechanically (heck, a ctfe string mixin wrapper could probably be built to
do it).

The potential downsides:

1. If you come across an @propery bug or limitation, you're SOL. This should
become less and less of an issue with time, though.

2. If one of the variables you converted is frequently-accessed, it could
cause a performance problem.

3. Small increase to storage requirements. Might potentially be a problem if
it's within templated or mixed-in code that gets instantiated many times.

At one point, I fiddled around with the idea of converting static ctors to
"staticThis()" and then having one real static ctor for the entire library
(assuming it's a library) that manually calls all the staticThis functions.
One problem with this is that it's easy to accidentally forget to call one
of the staticThis functions. The other big problem I found with this though,
especially for a library, is that it requires everyone importing your code
to always import through a single "import foo.all" module. If some user
skips that, then the static ctors won't get run. There might be some
possible workarounds for that, though:

- If the library has some primary interface that always gets used, then that
can easily check if the static ctors have run and error out if not. If the
primary interface is *always* the first part of your library used (or at
least the first of all the parts that actually rely on the static ctors
having run), then you could even run the static ctors right then instead of
erroring out. That's a lot of "if"s, though, so it may not be
widely-applicable.

- If you convert *all* static ctors to staticThis(), it might be possible to
stick the one main static ctor into a private utility module that gets
privately imported by all modules in the library. Then users can continue
importing whatever module(s) they want. But if you don't convert *all* of
the static ctors to staticThis, then you'll just re-introduce a cycle.

But if there's ever two separate libraries that have any interdependencies,
then the one-main-real static ctor (that calls all the staticThis() funcs)
will have to be shared between the two libraries. So overall, this approach
may be possible, but maybe only in certain cases, and can involve a lot of
changes.

I think the idea of a single static constructor in a main lib import module (let's call it lib.d), calling staticThis func in every module, is the right track. First, it is rather a good practice (I mean both from the designer and user points of view). Upon "it's easy to accidentally forget to call one of the staticThis functions": just systematically write one in every module, possibly empty at start. Then, from the main module, call staticThis on every imported module. This can be a cascade: staticThis if M calls staticThis of its own private dependencies. Shared submodules should still be init from lib, but even double init would be cheap thank the the bool flag.

Upon the case where users may need and import only part of your lib, then I guess obviously this mean some kind of *independency*, doesn't it? Else, all must be init-ed, and thus imported, anyway, don't you think? In which case they could as well import the main module in every case, and use only what they need.

Upon the last issue of common dependancies. If the problem of cyclic dependencies in static ctors is analog to circular imports, then there are 2 common strategies (here considering 2 modules importing each other): * Isolate a part of a module that requires its own module and another one, place it in a 3rd module which imports both. (This is a common issue for test cases.) * Conversely, isolate a part of a module that /is/ required by its own module and another one, place it in a 3rd module imported by both. (This is a common issue for tool features.) Strangely enough, this often solves the problem by splitting further instead of by merging. Though I don't know whether this is appropriate for your issue.

Denis
--
_________________
vita es estrany
spir.wikidot.com

Reply via email to