Search code examples
delphidata-structuresstructurerecord

Record with Array of itself in Delphi


Does anyone know how to do something like this?

type TFileRecord = record
    Name: String;
    CreationTimeStamp: DWORD;
    Subfiles: Array of TFileRecord;
end;

I am using Delphi 10.4.


Solution

  • In my Delphi 10.4.1 it is certainly possible to have that kind of declaration:

    unit TestUnit;
    
    interface
    
    type
      TFileRecord = record
        Name: String;
        CreationTimeStamp: Cardinal; //DWORD;
        Subfiles: Array of TFileRecord;
      end;
    
    Procedure TestTFR;
    
    implementation
    
    Procedure TestTFR;
    var
      TFR : TFileRecord;
    begin
      SetLength(TFR.SubFiles,2);
      SetLength(TFR.Subfiles[0].Subfiles,2);
    end;    
    
    end.
    

    This feature was introduced in Delphi 2009. Before that, the compiler complained about TFileRecord is not yet completely defined.

    As discussed in comments, the size of a dynamic array can only be the size of a pointer, which the compiler architects realised at that time. Knowing that, the finalization of the type declaration is of no importance for the single pass compiler.