[fpc-pascal] RTTI and generics: accessing specialized class instance

leledumbo leledumbo_cool at yahoo.co.id
Sat Oct 31 05:31:12 CET 2015


> TGenClass<> is not a full type, only TGenClass<Integer> is, so you need to
use - in your example - TSpecType(Obj)

Yes, I'm aware of this. The problem is I don't know TSpecType which I said
the last note. It could be any user defined specialization of TGenClass.
Back to above code, but closer to my actual complex code:

// my library

type
  TMyClass = class
    procedure mp;
  end;

  generic TGenClass<T> = class
    procedure gp;
  end;

procedure TMyClass.mp;
begin
  PropCount := GetPropList(Self,Props);
  for i := 0 to PropCount - 1 do begin
    Prop := Props^[i];
    PropName := Prop^.Name;
    case Prop^.PropType^.Kind of
      tkObject: begin
        Obj := GetObjectProp(Self,Prop); // this is where I may get
specialized TGenClass instance
        ...                                              // and I need to
call its gp method
      end;
    end;
  end;
end;

procedure TGenClass.gp;
begin
  ...
end;

// user of my library

type
  THisType = specialize TGenClass<TWhateverHeChoose>;

  THisClass = class(TMyClass)
  ...
  published
    ...
    HT: THisType read FHT write FHT; // this is what TMyClass.mp will get
above, which it doesn't know about
                                                      // because it's
declared later, much much later
  end;

I hope you can get the better picture. One solution is not to use generics
and return to good old inheritance, but I'm still thinking if there's a
solution without sacrificing the generics. I don't know how other languages
which has generics/templates solve this problem, though. Might be worth a
research.



--
View this message in context: http://free-pascal-general.1045716.n5.nabble.com/RTTI-and-generics-accessing-specialized-class-instance-tp5723011p5723014.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.



More information about the fpc-pascal mailing list