D 的“静态 if”是否是静态的?声明式还是程序式?
考虑以下代码:
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 if
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它是一个声明性功能,具有作为实现的副作用的过程属性。
It's a declarative feature that has procedural properties as a side effect of the implementation.
事情变得复杂了。它本质上是声明性的,但当
static if
引入新符号时,顺序仍然很重要。除此之外,我认为这并不重要,但正如您的示例所示,当您在static if
中引入新符号,而另一个static if
使用它的顺序肯定很重要。最近有一些讨论关于如何制作它尽可能一致和直观。因此,特别是在极端情况下,情况可能会在不久的将来发生变化。但我希望您的示例将继续触发
静态断言
。问题是,如果您颠倒static if
块的顺序,它是否会开始触发static assert
,而且我不确定这是否真的已经决定了。 编译器新闻组中对此的讨论不是完全是结论性的,而且有点难以遵循恕我直言,所以我不能肯定地说。但我预计至少在某些涉及引入新符号的static if
块的情况下,排序仍然很重要。编辑:
这是最近发布的 由 dmd 的主要贡献者之一撰写:
所以,希望这能澄清事情。
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 astatic if
, and anotherstatic 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 thestatic assert
if you reverse the order of thestatic 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 astatic if
block introducing a new symbol.EDIT:
This was recently posted by one of dmd's primary contributors:
So, hopefully that clarifies things.