如何获取泛型类型的默认值?
我使用的是 D 语言,并且希望获取泛型类型的默认值,类似于 default(T)
在 C# 中的工作方式。这可能吗?如果不是 - 有哪些可能的解决方法?
I'm using the D language, and would like to get the default value of a generic type, similar to the way default(T)
works in C#. Is this possible? If not - what are the possible workarounds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为
T.init
可能就是您正在寻找的。I think
T.init
might be what you're looking for.D 中的每个类型都有一个默认值。它是通过类型的
init
属性访问的。int.init
、float.init
、Object.init
等。对于模板化类型,它仍然是init
属性。例如,如果您有泛型类型T
,则它将是T.init
。init
通常是最接近该类型的错误值的。对于整型,它是0
。对于bool
,它是false
。对于浮点类型,它是NaN
。对于字符类型,它是\u00FF
。对于引用(即类)和指针,它是null
。对于结构体,它是其成员变量直接初始化的值。例如,在S.init
的情况下,将是
S
的一个实例,其a
为17
且b
是假
。特别值得注意的是,需要init
属性是 D 中的结构不能具有默认构造函数的原因。它们的默认状态 - 即它们的 init 属性 - 必须在编译时知道,而构造函数将在运行时运行,因此不能使用构造函数创建结构体的默认值,因此,虽然结构可以有构造函数,但它们不能有默认构造函数。对于枚举,
init
属性取决于它的枚举类型。诸如此类的清单常量将具有与其类型相同的
init
属性(在本例中为int
),因为您并未真正创建新类型。然而,对于实际创建新类型的枚举,例如enum E { a = 7, b = 17 };
默认值是枚举中的第一个值。在这种情况下,
E.init
将是a
。数组是它变得有点有趣的地方。动态数组和关联数组的
init
属性为null
。但是,当您为数组(无论是静态还是动态)分配内存时,每个元素都会初始化为其类型的init
属性。因此,对于数组,您既要考虑其init
值,又要考虑其元素的init
值。无论如何,获取类型默认值的通用方法是
T.init
,其中T
是您想要默认值的类型 - 无论是特定的类型类型或模板参数。Every type in D has a default value. It's accessed via the type's
init
property.int.init
,float.init
,Object.init
, etc. In the case of a templated type, it's still theinit
property. For example, if you had the generic typeT
, it would beT.init
.init
is generally the closest to an error value that the type has. For integral types, it's0
. Forbool
, it'sfalse
. For floating point types, it'sNaN
. For character types, it's\u00FF
. For references (i.e. classes) and pointers, it'snull
. And in the case of structs, it's whatever the value that its member variables are directly initialized to are. e.g. In the case ofS.init
would be an instance ofS
whosea
was17
andb
wasfalse
. Of particular note, the need for theinit
property is the reason that structs in D cannot have default constructors. Their default state - that is, theirinit
property - must be known at compile time, whereas a constructor would be run at runtime, so the default value of a struct can't be created with a constructor, and so, while structs can have constructors, they can't have default constructors.In the case of enums, the
init
property depends on the sort of enum that it is. A manifest constant such aswould have the same
init
property as its type (int
in this case), since you didn't really create a new type. However, for enums which actually create a new type, e.g.enum E { a = 7, b = 17 };
the default value is the first value in the enum. In this case,
E.init
would bea
.Arrays are where it gets a bit interesting though. The
init
property for dynamic arrays and associative arrays isnull
. However, when you allocate memory for an array (be it static or dynamic), each element is initialized to its type'sinit
property. So, with arrays, you have both the matter of theirinit
value and theinit
value of their elements.In any case, the generic way to get the default value of a type is
T.init
whereT
is the type that you want the default value of - be it a specific type or a template parameter.根据 codepad.org 重新枚举:
给出:
Re enum, according to codepad.org:
gives: