Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top