A Lombardo
A Lombardo

Reputation: 774

How to Declare and use a Generic TList of a Generic Record

I want to declare a generic record type such as

TMyGenericRecord<T, T1> = record
   X: <T>;
   Y: <T1>;
end;

Then I want to declare a TList of TMyGenericRecord but cannot seem to get the proper syntax for it.

TMyGenericList = TList<TMyGenericRecord<T, T1>>???

Is this even possible?

Upvotes: 3

Views: 1040

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

I think you want to write it like this:

type
  TMyGenericRecord<T1, T2> = record
    X: T1;
    Y: T2;
  end;

  TMyGenericList<T1, T2> = class(TList<TMyGenericRecord<T1, T2>>);

You can then instantiate the type like this, for example:

var
  List: TMyGenericList<Integer, string>;

You can then declare a record that is compatible with this list like so:

var
  Rec: TMyGenericRecord<Integer, string>;

At which point

List.Add(Rec);

will compile. Although clearly you'd need to construct an instance of the list.

Upvotes: 8

Related Questions