I’m trying to cast a returned base object to it’s specific generic type. The code below should work I think but generates an internal compiler error, is there another way to do this?
type
TPersistGeneric<T> = class
private
type
TPointer = ^T;
public
class function Init : T;
end;
class function TPersistGeneric<T>.Init : T;
var
o : TXPersistent; // root class
begin
case PTypeInfo(TypeInfo(T))^.Kind of
tkClass : begin
// xpcreate returns txpersistent, a root class of T
o := XPCreate(GetTypeName(TypeInfo(T))); // has a listed of registered classes
result := TPointer(pointer(@o))^;
end;
else
result := Default(T);
end;
end;
I’m using a typecast helper class that does the typecasts and also checks if the two classes are compatible.
Here is the class: