title: How can I cast an object to a generic?
body: <p>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?</p>
  
  <pre><code>type
    TPersistGeneric&lt;T&gt; = class
    private
    type
      TPointer = ^T;
    public
      class function  Init : T;
    end;
  
  class function  TPersistGeneric&lt;T&gt;.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;
  </code></pre>