D 的“静态 if”是否是静态的?声明式还是程序式?

发布于 2024-12-12 17:26:14 字数 460 浏览 0 评论 0原文

考虑以下代码:

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

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

我最初的理解是,在 D 中声明的顺序(在全局范围内)并不重要

但是,在这种情况下,static if 的顺序s 决定了程序能否编译。

因此,D 的编译时求值阶段是程序性功能(如 C/C++)、声明性功能还是其他功能?目前是什么,计划是什么(如果两者不同)?


编辑:

我刚刚意识到,问题甚至还没有结束。如果 static if 使用 .tupleof 枚举当前模块的成员并产生相同类型的问题,会发生什么情况?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

被翻牌 2024-12-19 17:26:14

它是一个声明性功能,具有作为实现的副作用的过程属性。

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

深爱不及久伴 2024-12-19 17:26:14

事情变得复杂了。它本质上是声明性的,但当 static if 引入新符号时,顺序仍然很重要。除此之外,我认为这并不重要,但正如您的示例所示,当您在 static if 中引入新符号,而另一个 static if 使用它的顺序肯定很重要。

最近有一些讨论关于如何制作它尽可能一致和直观。因此,特别是在极端情况下,情况可能会在不久的将来发生变化。但我希望您的示例将继续触发静态断言。问题是,如果您颠倒 static if 块的顺序,它是否会开始触发 static assert,而且我不确定这是否真的已经决定了。 编译器新闻组中对此的讨论不是完全是结论性的,而且有点难以遵循恕我直言,所以我不能肯定地说。但我预计至少在某些涉及引入新符号的 static if 块的情况下,排序仍然很重要。

编辑:

这是最近发布的 由 dmd 的主要贡献者之一撰写:

目前,编译时求值的顺序尚未定义; DMD
目前按词汇顺序模糊地执行,但计划这样做
在不久的将来发生变化。 'static if' 和 'mixin' 将被评估
在做其他事情之前,按照词汇顺序。然后,
其他一切都将按需评估。

除了“static if/mixin”传递之外,编译还可以在
并行(尽管当前的实现还没有做到这一点)
表示没有排序(多个项目可能完成编译
同时)。

所以,希望这能澄清事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文