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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With