Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2024-12

Forward Record

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Amir

Hi,

  I have a use-case where I needed to forward declare some records. The FPC documentation
<https://www.freepascal.org/docs-html/ref/refse16.html> says "Note that a forward type declaration is only possible with pointer types and classes, not with other types".

  I wonder why? Is there any technical difference between classes and records for the purpose of forward declaration?

My use-case was to define a few records like the following:

type
   TData = record
     ....
     function ToA: TA;
    end;
   TA = record
      Data: TData;
      ....
   end;

I know I can define classes instead of record, but I do not want to do so, since I need to "Free" the objects.

Amir

Reply by email

Sven Barth
to Amir

Amir via fpc-devel schrieb am Do., 5. Dez.
2024, 00:57:

I wonder why? Is there any technical difference between classes and records for the purpose of forward declaration?

Yes, there is. Classes are implicit pointer types, thus when you have a field that is of type class then it's size will always be the size of a Pointer. Records (and TP-style objects) have a different size depending in their fields and this size needs to be known when you use a record (or TP-style object) directly as a field.

Show quoted text 16 lines
My use-case was to define a few records like the following: type TData = record .... function ToA: TA; end; TA = record Data: TData; .... end; I know I can define classes instead of record, but I do not want to do so, since I need to "Free" the objects.

You could add a record helper below the declaration of TA for TData instead of declaring ToA inside of TData:

=== code begin ===

  TDataHelper = record helper for TData
    function ToA: TA;
   end;

=== code end ===

The usage is the same as if you had declared the method inside of TData.

Regards,
Sven

Reply by email