Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 894
RBA Avatar asked Jan 20 '26 01:01

RBA


1 Answers

You need to constraint the generic parameter in order to check for nil. For example:

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

That way T must be TObject or any descendant of it, so you can check for nil.

Without the constraint T can be integer or any other value type that doesn't support nil.

like image 171
Wosi Avatar answered Jan 22 '26 16:01

Wosi