digitalmars.D - Cyclic depency with class static ctor
- Frank Benoit (keinfarbton) (10/10) Oct 26 2006 I have project with many classes which also sometimes have static ctors.
- Jarrett Billingsley (10/20) Oct 26 2006 Put all the classes in one file. No, seriously. That's the proposed
- Frits van Bommel (18/30) Oct 26 2006 http://www.digitalmars.com/d/module.html#staticorder (under "Order of
- Carlos Santander (6/19) Oct 26 2006 To get a bit of help with this, check out dimple: http://www.shfls.org/w...
- Walter Bright (5/18) Oct 26 2006 Yes. It isn't possible for the language to really determine the
- BCS (21/26) Oct 27 2006 If the rules were changed so that static this functions may only depend
-
Walter Bright
(2/11)
Oct 29 2006
I'd rather have fewer order dependencies, not more
. - Kristian (9/20) Oct 30 2006 Well, generally in programming things must be done in a specific order.....
- Walter Bright (3/10) Oct 30 2006 I don't think new syntax is justified because Frank's workaround handles...
- Jarrett Billingsley (5/15) Oct 30 2006 Except that it makes static ctors entirely useless, since you're just
- Walter Bright (16/32) Oct 30 2006 It can appear that way, but there is a crucial point about static
- Jarrett Billingsley (31/44) Oct 30 2006 That's true. Though more times than not, I've run into the cyclic impor...
I have project with many classes which also sometimes have static ctors. Now I get the runtime error "cyclic dependency in module ...". Is that a bug in the d runtime system, or is it just not documented, that this runtime error is also possible for static ctors? Now I have these 300+ ported classes, in 300+ modules. And they import each other and many of them have a static ctor. What can I do? How can I resolve the problems? I can think of the possibility to replace all "static this()" with a "public static void static_this()" and make a global module where i call all these ctor in a manual defined order. Is that the way to go?
Oct 26 2006
"Frank Benoit (keinfarbton)" <benoit tionex.removethispart.de> wrote in message news:ehrblq$2do6$1 digitaldaemon.com...I have project with many classes which also sometimes have static ctors. Now I get the runtime error "cyclic dependency in module ...". Is that a bug in the d runtime system, or is it just not documented, that this runtime error is also possible for static ctors? Now I have these 300+ ported classes, in 300+ modules. And they import each other and many of them have a static ctor. What can I do? How can I resolve the problems?Put all the classes in one file. No, seriously. That's the proposed solution.I can think of the possibility to replace all "static this()" with a "public static void static_this()" and make a global module where i call all these ctor in a manual defined order. Is that the way to go?I've done pretty much the equivalent in a lot of my projects. It's just not really possible to mash together some modules which import each other, without putting most of the project into one module. This error could probably be avoided with a smarter compiler, but now we're getting into QOI (quality of implementation) issues, and maybe not something which could be enforced by the spec..
Oct 26 2006
Frank Benoit (keinfarbton) wrote:I have project with many classes which also sometimes have static ctors. Now I get the runtime error "cyclic dependency in module ...". Is that a bug in the d runtime system, or is it just not documented, that this runtime error is also possible for static ctors?http://www.digitalmars.com/d/module.html#staticorder (under "Order of Static Construction") ----- 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 it's definitely documented. Not as a bug, but as a limitation. The reason for this is probably so that static constructors can assume imported modules have been properly initialized.Now I have these 300+ ported classes, in 300+ modules. And they import each other and many of them have a static ctor. What can I do? How can I resolve the problems? I can think of the possibility to replace all "static this()" with a "public static void static_this()" and make a global module where i call all these ctor in a manual defined order. Is that the way to go?If you want to go this way, remember you don't have to do this for *all* modules, just the ones involved in a cyclic dependency whose static constructors don't depend on the other ones having been run yet. Then call it from the start of the static constructors of any modules importing it. If there's multiple such modules, make sure it doesn't matter if it's run multiple times, since you can't rely on any module being initialized before another (unless it's imported by it).
Oct 26 2006
Frits van Bommel escribió:Frank Benoit (keinfarbton) wrote:To get a bit of help with this, check out dimple: http://www.shfls.org/w/d/dimple/ The generated graphic shows cyclic dependencies. However, I don't know how well it works with some of the recent additions to D (static import, etc.) -- Carlos Santander BernalNow I have these 300+ ported classes, in 300+ modules. And they import each other and many of them have a static ctor. What can I do? How can I resolve the problems? I can think of the possibility to replace all "static this()" with a "public static void static_this()" and make a global module where i call all these ctor in a manual defined order. Is that the way to go?If you want to go this way, remember you don't have to do this for *all* modules, just the ones involved in a cyclic dependency whose static constructors don't depend on the other ones having been run yet.
Oct 26 2006
Frank Benoit (keinfarbton) wrote:I have project with many classes which also sometimes have static ctors. Now I get the runtime error "cyclic dependency in module ...". Is that a bug in the d runtime system, or is it just not documented, that this runtime error is also possible for static ctors? Now I have these 300+ ported classes, in 300+ modules. And they import each other and many of them have a static ctor. What can I do? How can I resolve the problems? I can think of the possibility to replace all "static this()" with a "public static void static_this()" and make a global module where i call all these ctor in a manual defined order. Is that the way to go?Yes. It isn't possible for the language to really determine the dependencies, so it plays it safe and tries to get all the static ctors run before the module(s) static ctors that import them. With cyclic imports and static ctors in those cycles, this is not possible.
Oct 26 2006
Walter Bright wrote:Yes. It isn't possible for the language to really determine the dependencies, so it plays it safe and tries to get all the static ctors run before the module(s) static ctors that import them. With cyclic imports and static ctors in those cycles, this is not possible.If the rules were changed so that static this functions may only depend on modules imported before them, then a lot of cases could be made to work. <code name="a.d> static this(){writef("A\n");} </code> <code name="b.d> import a; import c; static this(){writef("B\n");} </code> <code name="c.d> import a; static this(){writef("C\n");} import b; </code> <output> A C B </output>
Oct 27 2006
BCS wrote:Walter Bright wrote:I'd rather have fewer order dependencies, not more <g>.Yes. It isn't possible for the language to really determine the dependencies, so it plays it safe and tries to get all the static ctors run before the module(s) static ctors that import them. With cyclic imports and static ctors in those cycles, this is not possible.If the rules were changed so that static this functions may only depend on modules imported before them, then a lot of cases could be made to work.
Oct 29 2006
On Mon, 30 Oct 2006 06:48:55 +0200, Walter Bright <newshound digitalmars.com> wrote:BCS wrote:Well, generally in programming things must be done in a specific order... ;) 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();Walter Bright wrote:I'd rather have fewer order dependencies, not more <g>.Yes. It isn't possible for the language to really determine the dependencies, so it plays it safe and tries to get all the static ctors run before the module(s) static ctors that import them. With cyclic imports and static ctors in those cycles, this is not possible.If the rules were changed so that static this functions may only depend on modules imported before them, then a lot of cases could be made to work.
Oct 30 2006
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.
Oct 30 2006
"Walter Bright" <newshound digitalmars.com> wrote in message news:ei5kj0$1p2m$2 digitaldaemon.com...Kristian wrote: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.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.
Oct 30 2006
Jarrett Billingsley wrote:"Walter Bright" <newshound digitalmars.com> wrote in message news:ei5kj0$1p2m$2 digitaldaemon.com...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.Kristian wrote: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.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.
Oct 30 2006
"Walter Bright" <newshound digitalmars.com> wrote in message news:ei6795$2d76$1 digitaldaemon.com...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.That's true. Though more times than not, I've run into the cyclic import problem..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.It just seems like unnecessary cruft to have to do what Frank mentioned, i.e. to create a "static ctor module" which calls all the static initializations for all the modules in existence. It seems like the compiler would be able to make a much better judgement call about which static ctors depended upon which modules, and would be able to issue a compile-time error if there really were a cyclic static ctor dependency (which, 99% of the time, there _isn't_, which is why it seems completely superfluous to have to manually initialize the modules!). It makes me angry that I can't do something as simple as: module a; import b; int x; static this() { x = 5; } ............... module b; import a; int y; static this() { y = 10; } There is obviously no interdependency between the modules for static initialization, but D throws an error at runtime nonetheless. It seems almost stupid.
Oct 30 2006