質問

Consider the following types

type
  TRecs = array[0..100000] of TRec;
  PRecs = ^TRecs;

  TRecObject = class
  private
    fRecs: PRecs;
  public
    constructor Create;
    property    Recs: PRecs read fRecs;
  end;

I would like to make TRec a generic parameter. The problem is that I need to place outside of the class scope. Because something like

 T<MyType>Object = class
 private
   fRecs: ^array[0..100000] of MyType;
 public
    property    Recs: ^array[0..100000] of MyType read fRecs
 end

is not possible.

Making PRecs a parameter also is not an option because there is TRec-related code in my actual object.

Is there a solution in modern Object Pascal? And if not, just curious is there any other generic-enabled language that can solve something like this?

役に立ちましたか?

解決

I'm not entirely sure I understand your question, but I think you are looking for something like this:

type
  TMyObject<T> = class
  public
    type
      PArr = ^TArr;
      TArr = array[0..100000] of T;
  private
    fRecs: PArr;
  public
    property Recs: PArr read fRecs
  end;

That said, I can't see the point of that class. You could just use TList<T> from Generics.Collections.

And if your need an array, then you can use a dynamic array: TArray<T> or array of T, as you prefer.

他のヒント

You got your generic syntax a bit muddled up. Try this:

  TRecArray<T> = array[0..100000] of T;

  TGenericRecObject<T> = class
  private
    FRecs: TRecArray<T>;
  public
    property Recs: TRecArray<T> read FRecs;
  end;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top