如何获取泛型类型的默认值?

发布于 2024-12-02 05:24:07 字数 96 浏览 1 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久隐师 2024-12-09 05:24:07

我认为 T.init 可能就是您正在寻找的。

I think T.init might be what you're looking for.

擦肩而过的背影 2024-12-09 05:24:07

D 中的每个类型都有一个默认值。它是通过类型的 init 属性访问的。 int.initfloat.initObject.init 等。对于模板化类型,它仍然是 init 属性。例如,如果您有泛型类型 T,则它将是 T.init

init 通常是最接近该类型的错误值的。对于整型,它是0。对于bool,它是false。对于浮点类型,它是NaN。对于字符类型,它是 \u00FF。对于引用(即类)和指针,它是null。对于结构体,它是其成员变量直接初始化的值。例如,在 S.init 的情况下,

struct S
{
    int a = 17;
    bool b;
}

将是 S 的一个实例,其 a17b。特别值得注意的是,需要 init 属性是 D 中的结构不能具有默认构造函数的原因。它们的默认状态 - 即它们的 init 属性 - 必须在编译时知道,而构造函数将在运行时运行,因此不能使用构造函数创建结构体的默认值,因此,虽然结构可以有构造函数,但它们不能有默认构造函数。

对于枚举,init 属性取决于它的枚举类型。诸如此类的清单常量

enum i = 7;

将具有与其类型相同的 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 the init property. For example, if you had the generic type T, it would be T.init.

init is generally the closest to an error value that the type has. For integral types, it's 0. For bool, it's false. For floating point types, it's NaN. For character types, it's \u00FF. For references (i.e. classes) and pointers, it's null. 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 of

struct S
{
    int a = 17;
    bool b;
}

S.init would be an instance of S whose a was 17 and b was false. Of particular note, the need for the init property is the reason that structs in D cannot have default constructors. Their default state - that is, their init 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 as

enum i = 7;

would 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 be a.

Arrays are where it gets a bit interesting though. The init property for dynamic arrays and associative arrays is null. However, when you allocate memory for an array (be it static or dynamic), each element is initialized to its type's init property. So, with arrays, you have both the matter of their init value and the init value of their elements.

In any case, the generic way to get the default value of a type is T.init where T is the type that you want the default value of - be it a specific type or a template parameter.

傲影 2024-12-09 05:24:07

根据 codepad.org 重新枚举:

enum Foo {
  a = 3,
  b = 2,
}

import std.stdio;
void main() { writef("%d", Foo.init); }

给出:

3

Re enum, according to codepad.org:

enum Foo {
  a = 3,
  b = 2,
}

import std.stdio;
void main() { writef("%d", Foo.init); }

gives:

3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文