6
C#6中有一个“nameof”运算符(https://msdn.microsoft.com/library/dn986596.aspx)。 D有类比吗?或者一些构想来模拟它?C中D的运算符名称?
C#6中有一个“nameof”运算符(https://msdn.microsoft.com/library/dn986596.aspx)。 D有类比吗?或者一些构想来模拟它?C中D的运算符名称?
我相信stringof
的功能很大。例如,d模拟到第一C#示例在该链接是:
void f(string s) {
if (s == null) throw new Exception(s.stringof ~ " is null!");
}
还有std.traits.fullyQualifiedName
。它做什么,它说就可以了:
module mymodule;
import std.traits : fullyQualifiedName;
class MyClass { int myvar; }
pragma(msg, MyClass.myvar.stringof); // myvar
pragma(msg, fullyQualifiedName!(MyClass.myvar)); // mymodule.MyClass.myvar
作为第一环节所指出的,fullyQualifiedName
可能更适合于编译时代码生成,它有助于要尽可能具体,以避免与本地符号冲突。