I created the following simple class in my code and I get "TObject is not a valid constraint" error message.

  TTest = class
    class function Test<T:TObject>(Dummy: integer): string;
  end;
  ... 
  class function TTeste.Teste<T>(Dummy: integer): string;
  begin
    Result := 'Compiled';
  end;

What I'm doing wrong? Is this possible? (You can include option available on later versions than D2010, since we'll receive our XE2 licenses in the next 2 weeks).

有帮助吗?

解决方案

Try the class constraint:

TTest = class
  class function Test<T:class>(Dummy: integer): string;
end;

It might not only accept normal classes, but also interfaces with that constraint(in C# it does, and Delphi generics imitate C# generics).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top