变量转储程序模板函数不考虑范围



我有一个方便的dump函数(从互联网复制,很可能是从 https://forum.dlang.org/复制的),我用它来研究变量。

我只是注意到它并不在所有情况下都尊重范围(见下文)。为什么以及如何修复它以获得预期的结果?还是该功能从根本上存在缺陷? 当我学习D时,dump非常有价值。

我在Linux上使用DMD64 D Compiler v2.083.0。

使用-debug编译时的预期结果:

(immutable(int) x = 1)
(immutable(immutable(char)[]) x = A)
(immutable(double) x = 1.1)

而是得到:

(immutable(int) x = 1)
(immutable(int) x = 1)
(immutable(double) x = 1.1)

代码:

void dump(alias variable)()
{
import std.stdio : writefln;
writefln("(%s %s = %s)",
typeid(typeof(variable)),
variable.stringof,
variable);
}
void main()
{
{
immutable x = 1; debug dump!x;
}
{
immutable x = "A"; debug dump!x;
}
{
void f() { immutable x = 1.1; debug dump!x; }
f();
}
}

看起来你遇到了这个编译器错误:

https://issues.dlang.org/show_bug.cgi?id=13617

函数局部符号没有唯一名称,并且与同级作用域中的符号冲突

从错误报告中派生的最小示例来说明问题:

bool isInt(alias x)() { return is(typeof(x) == int); }
void main() {
{ int   a; static assert(isInt!a); }
{ float a; static assert(isInt!a); } // passes unexpectly (compiler bug)
}

上面的代码错误地编译,即使第二个static assert编译时应该失败。

最新更新