Franz
Franz

Reputation: 2031

What does the E2511 Type parameter 'T' must be a class type compiler error mean?

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?

Upvotes: 6

Views: 1415

Answers (1)

David Heffernan
David Heffernan

Reputation: 613481

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;

Upvotes: 11

Related Questions