On Tuesday, 12 September 2017 at 10:08:11 UTC, Moritz Maxeiner wrote:
On Tuesday, 12 September 2017 at 09:11:20 UTC, Joseph wrote:
I have two nearly duplicate files I added a static this() to initialize some static members of an interface.

On one file when I add an empty static this() it crashes while the other one does not.

The exception that happens is
Cyclic dependency between module A and B.

Why does this occur on an empty static this? Is it being ran twice or something? Anyway to fix this?

The compiler errors because the spec states [1]

Each module is assumed to depend on any imported modules being statically constructed first

, which means two modules that import each other and both use static construction have no valid static construction order.

One reason, I think, why the spec states that is because in theory it would not always be possible for the compiler to decide the order, e.g. when executing them changes the ("shared") execution environment's state:

---
module a;
import b;

static this()
{
    // Does something to the OS state
    syscall_a();
}
---

---
module b;
import a;

static this()
{
    // Also does something to the OS state
    syscall_b();
}
---

The "fix" as I see it would be to either not use static construction in modules that import each other, or propose a set of rules for the spec that define a always solvable subset for the compiler.

[1] https://dlang.org/spec/module.html#order_of_static_ctor

The compiler shouldn't arbitrarily force one to make arbitrary decisions that waste time and money.

My solution was to turn those static this's in to functions and simply call them at at the start of main(). Same effect yet doesn't crash. The compiler should only run the static this's once per module load anyways, right? If it is such a problem then some way around it should be included: @force static this() { } ? The compiler shouldn't make assumptions about the code I write and always choose the worse case, it becomes an unfriendly relationship at that point.

Reply via email to