[fpc-pascal] Are record fields zeroed on declaration of a record?

Michael Van Canneyt michael at freepascal.org
Thu Oct 8 23:00:32 CEST 2020



On Thu, 8 Oct 2020, Ryan Joseph via fpc-pascal wrote:

>
>
>> On Oct 8, 2020, at 5:14 AM, gabor via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>> 
>> You can define typed constant and assign it to variable.
>> 
>> type
>>  TMyRecord = record
>>    X, Y: Integer;
>>    S: String;
>>  end;
>> 
>> const
>>  MYDEFREC: TMyRecord = (X: 1; Y: 2; S: 'ABC');
>
> I never understood why we can't initialize fields at init time for
> records.  Pascal seems to be the only language that doesn't support this
> in 2020.  Thinking of C++, C#, PHP, Swift and many more all let you do
> this.  Not even Delphi supports this AKAIK.

Of course you can:

var
   x : record a,b : integer; end = (a:1;b:3);


begin
end.

This is identical to how C++ does it. From cppreference.com:

typedef struct { int k; int l; int a[2]; } T;
T x = {.l = 43, .k = 42, .a[1] = 19, .a[0] = 18 };

Michael.


More information about the fpc-pascal mailing list