RBA
RBA

Reputation: 12584

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?

Upvotes: 3

Views: 593

Answers (1)

Wosi
Wosi

Reputation: 45341

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.

Upvotes: 4

Related Questions