[fpc-pascal] Are record fields zeroed on declaration of a record?
Ryan Joseph
genericptr at gmail.com
Fri Oct 9 16:58:05 CEST 2020
> On Oct 8, 2020, at 4:58 AM, Bo Berglund via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>
> OK,
> is it possible to define the TMyRecord with default values so that
> when I do Default(TMyRecord) it will be non-zero defaults written
> instead of zeros?
You need to make a method for tha. You can't even use constructors because they must have parameters for some reason but you can use a static class function (this requires advanced records mode switch of course). There is also an initialize operator that is in 3.2.0 I believe.
Sven did say one time that he MAY consider allowing a record initialization syntax inside code blocks (see below). That would be a nice option but they require all fields to be initialized. Having default values like C++ would be best though.
type
TmyRec = record
a, b, c: integer;
d, e: double;
class function Create: TMyRec; static; // this syntax works.
class operator Initialize(var self: TMyRec); // init operator gets called when the record is declared
end;
var
charlie: TmyRec = (a:2; b:3; e:3.1415);
begin
charlie := TmyRec(a:2; b:3; e:3.1415); // this would be a nice addition to avoid constructors
Regards,
Ryan Joseph
More information about the fpc-pascal
mailing list