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

Bo Berglund bo.berglund at gmail.com
Fri Oct 9 07:27:49 CEST 2020


On Thu, 8 Oct 2020 23:00:32 +0200 (CEST), Michael Van Canneyt via
fpc-pascal
<fpc-pascal at lists.freepascal.org> wrote:

>
>
>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 };
>

If you do that can you be selective and initialize only some fields or
must you list all or none?

Like:

type
  TmyRec = record
    a, b, c: integer;
    d, e: double
  end = (a:2; b:3; e:3.1415);

var
  charlie: TmyRec;
begin
  Default(charlie);

What will charlie.c and charlie.d contain now?
And are a, b and e left alone at the default values?

Or are all zero?


-- 
Bo Berglund
Developer in Sweden



More information about the fpc-pascal mailing list