gpt4 book ai didi

delphi - 如何在Delphi中定义具有自引用泛型约束的类?

转载 作者:行者123 更新时间:2023-12-03 14:54:28 31 4
gpt4 key购买 nike

article所示,可以在 C# 中定义:

public class MyTreeNode<T> where T: MyTreeNode<T>
{

}

但是,在 Delphi 中,以下代码无法编译并提示“E2003 未声明的标识符:'MyTreeNode<>'”:

type
TMyTreeNode<T: TMyTreeNode<T>> = class
end;

正式术语似乎是 "self-referencing generics constraint" 。我想知道如何在 Delphi 中执行此操作?

PS:

Another useful article as David pointed out

Relevent SO post regarding Delphi and covariance / contravariance

Wikipedia page of co/contra-variance .

最佳答案

Delphi 中没有办法表达这样的通用约束。此外,我相信在这种约束下,没有类型安全的方法可以实现 C# 中可以实现的目标。

关于delphi - 如何在Delphi中定义具有自引用泛型约束的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23245741/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com