Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to generic record

Note that this is not a duplicate of Pointer to generic type. It's a followup question.

I know it is possible to define a pointer to any generic type.
It just that Delphi makes it complicated. It was meant to be impossible, but due to a compiler bug the option slipped through.
This is what the linked question answers.

My question is:

How do I define a pointer to a generic record without encapsulating it in a surrounding class?

Example code:

TGenericRecord<T> = record
  Data: integer;
  Procedure SomeMethod; inline; <<<< inlining is vital here. 
end;

I want to get a type safe pointer to TGenericRecord.
I do not want to wrap the record in a surrounding class because in my experiments I've found that that disables the inlining.

How do I get a typesafe generic pointer to this record.

Use case

{class} function create(size: integer): PGenericRecord{<T>}

I want to be able to create records on the heap in addition to the stack.

like image 646
Johan Avatar asked Jul 19 '26 14:07

Johan


1 Answers

I think your best bet probably looks like this:

type 
  TMyStaticClass<T> = class
  public
    type
      TRec = record
        ....
      end;
      PRec = ^TRec;
  public
    class function NewRec: PRec; static;
  end;

I don't have a compiler handy to check whether or not this even compiles but I feel that it should.....

like image 79
David Heffernan Avatar answered Jul 22 '26 06:07

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!