Issue with generic compatibility

Viewed 200

Using Delphi 10.3 Update 3. I'm getting a compile error E2010 Incompatible types 'Pointer' and 'T' on this simple bit of code:

interface

TFeedList<T: class> = class(TObjectList<T>)
public
  function AsTab: String;
end;

implementation

function TFeedList<T>.AsTab: String;
var
  r: T;
  rp : TRTTIProperty;
  ctx : TRTTIContext;
begin
  for r in Self do
  begin
    // snip
    for rp in ctx.GetType(T).GetProperties do
    begin
      // snip
      Result := rp.GetValue(r).AsString;  // throws compiler error
    end;
  end;
end;

Ignore the logic issues here, I've snipped out parts of my code.

I tried casting r as Pointer(r), but this threw a runtime error saying that r was not a pointer.

Edit: I worked around this by putting the RTTI code into an ancestor class for T (changed to TFeedList<T: TAncestor>), and then iterating the properties on Self. TFeedList.AsTab then collects the outputs of each item in its list by calling TAncestor.AsTab.

If anyone has some insight, I'd still like to know why the original code wouldn't compile.

0 Answers
Related