简体   繁体   English

Delphi - 通用类型检查是否已创建

[英]Delphi - generic type check if is created

I have the following class definition 我有以下类定义

  TBase<T> = class
  public
    class var Inst: T;
    class function GetClone: T;
  end;

And I want to check if the class var Inst is assigned. 我想检查是否已分配类var Inst。

class function TBase<T>.GetClone: T;
begin
 if TBase<T>.Inst = nil then //- error here. Trying with Assigned(TBase<T>.Inst) is also nor recognized.
    TBase<T>.Inst := TBase<T>.Create;
end;

How can I check if my class variable is assigned? 如何检查我的类变量是否已分配?

You need to constraint the generic parameter in order to check for nil . 您需要约束泛型参数以检查nil For example: 例如:

TBase<T: class> = class //...

That way T must be TObject or any descendant of it, so you can check for nil . 那样T必须是TObject或它的任何后代,所以你可以检查nil

Without the constraint T can be integer or any other value type that doesn't support nil . 没有约束, T可以是integer或任何其他不支持nil值类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM