6

Following my previous question, I am attempting to compile the code from one of the answers there.

 type 
   TSearchableObjectList<T> = class(TObjectList<T>)
   end;

The compiler will not compile this and reports this error message:

[dcc32 Error]: E2511 Type parameter 'T' must be a class type

What does this error message mean, and how should I fix the code?

Community
  • 1
  • 1
Franz
  • 1,883
  • 26
  • 47
  • 2
    Why not add a comment to the original question? BTW I have edited the answer to fix that – Sir Rufo Jul 19 '13 at 08:15
  • 4
    @SirRufo That's a good suggestion. Anyway, I've edited the question to make it more general, and edited the title to include the error message. This should make it more searchable. Thanks for the edit to my answer also. – David Heffernan Jul 19 '13 at 08:28

1 Answers1

11

TObjectList<T> includes a generic constraint that T is a class. The type declaration is as follows:

type
  TObjectList<T: class> = class(TList<T>)
    ...
  end;

You might think that constraints are inherited, but that is not the case. And so you need to include the constraint in your class. Specify the constraint like so:

type
  TSearchableObjectList<T: class> = class(TObjectList<T>)
    ...
  end;
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490