1

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?

Maksee
  • 2,311
  • 2
  • 24
  • 34

2 Answers2

3

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • :) you were quicker. and closer to the question with that pointer stuff. – Marjan Venema Nov 15 '12 at 14:05
  • You understood it perfectly :) – Maksee Nov 15 '12 at 14:43
  • There is no generics.collections in FPC. In FPC there are some generic containers in unit fgl though, Delphi's generics.collections is not compatible to fgl. – Marco van de Voort Nov 15 '12 at 19:21
  • @marco I think the question is about delphi and is mis-tagged. But I don't really know. Not sure how a question can be about both. – David Heffernan Nov 15 '12 at 20:13
  • @David_Heffernan, I think as long as the fpc team considers delphi compatibility a high priority, asking about both makes sense. As for your solution, it worked well in fpc – Maksee Nov 16 '12 at 08:05
  • OK. I understand now. Tagging both Delphi and fpc can be confusing. Sometimes it means the asker is using fpc but wants delphi devs to read the Q. Perhaps clarification in the Q would help. Anyway, I think it's all good here! – David Heffernan Nov 16 '12 at 08:36
  • Note I commented to the List suggestion, not the rest. Btw, generics are substantially better in FPC/trunk then in released versions – Marco van de Voort Nov 16 '12 at 08:39
  • @Marco How do fpc generics compare with Delphi? Does fpc have fewer compiler bugs?! – David Heffernan Nov 16 '12 at 10:38
  • Then what? D2009?, sure. :-) FPC originally had a more C++ like approach to generics without constraints. (this is pre D2009) Then Delphi added generics too, in an incompatible way, and the current situation is a hybrid where constraints are parsed but ignored. (at the expense of finding many problems only at specialization). At least it doesn't have bugs like QC 99703 :-) – Marco van de Voort Nov 16 '12 at 11:47
  • @MarcovandeVoort Even in XE3 there are plenty of generics compiler bugs. It seems like a total bug factory. – David Heffernan Nov 16 '12 at 11:48
  • 1
    Generics is a difficult subject. Basically you take the language and add a whole dimension to everything. It took the various C++ compilers also a long time to get it to a stable level. – Marco van de Voort Nov 16 '12 at 14:18
2

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;
Marjan Venema
  • 19,136
  • 6
  • 65
  • 79
  • .... but your dynamic type declared outside the class is quite nice. Also, question seems to want to use pointers. I think that's because @Maksee hasn't come across dynamic arrays yet. – David Heffernan Nov 15 '12 at 14:07
  • You could be right. I avoid pointers if I can and dynamic array types are indeed wonderful in that respect. – Marjan Venema Nov 15 '12 at 14:10
  • That's correct, I need pointers. Let's say for historical reasons :) – Maksee Nov 15 '12 at 14:42