【问题标题】:Variable dumpper template function doesn't respect scope变量转储模板功能不尊重范围
【发布时间】:2019-07-20 07:47:19
【问题描述】:

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

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

我在 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();
  }
}

【问题讨论】:

    标签: d


    【解决方案1】:

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

    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 应该在编译时失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 2018-12-11
      • 2016-04-29
      • 2017-03-16
      • 1970-01-01
      相关资源
      最近更新 更多