Question

Consider the following code:

static if (!is(MyStruct))
{
    struct MyStruct
    {
    }
}

static if (is(MyStruct))
{
    static assert(0);
}

My original understanding has been that the order of declarations (in global scope) does not matter in D.

However, in this case, the order of the static ifs makes the difference between whether or not the program compiles.

Is D's compile-time evaluation stage, therefore, a procedural feature (like C/C++), a declarative feature, or something else? What is it currently, and what is it planned to be (if the two are different)?


Edit:

I just realized, the problem doesn't even end here. What happens of a static if uses .tupleof to enumerate the members of the current module, and create the same type of problem?

Was it helpful?

Solution

It's a declarative feature that has procedural properties as a side effect of the implementation.

OTHER TIPS

It gets complicated. It's essentially declarative, but order can still matter when a static if introduces a new symbol. Aside from that, I don't believe that it ever matters, but as your example shows, when you introduce a new symbol in a static if, and another static if uses it, the order definitely can matter.

There has been some discussion recently about how to make it as consistent and intuitive as possible. So, particularly in corner cases, the situation may change in the near future. But I would expect that your example would continue to trigger the static assert. The question is whether it will start to trigger the static assert if you reverse the order of the static if blocks, and I'm not sure that that's really been decided yet. The discussion on it in the compiler's newsgroup isn't entirely conclusive and a bit hard to follow IMHO, so I can't say for sure. But I expect that ordering will still matter in at least some cases which involve a static if block introducing a new symbol.

EDIT:

This was recently posted by one of dmd's primary contributors:

At present, the order of compile-time evaluation is not defined; DMD currently does it vaguely in lexical order but that is planned to change in the near future. 'static if' and 'mixin' will be evaluated in lexical order, before anything else is done. Afterwards, everything else will be evaluated on-demand.

Apart from the "static if/mixin" pass, compilation can proceed in parallel (though the current implementation doesn't yet do this) which means there's no ordering (multiple items may complete compilation simultaneously).

So, hopefully that clarifies things.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top