[fpc-pascal] Managed record types
Sven Barth
pascaldragon at googlemail.com
Sun Nov 3 11:06:53 CET 2024
Thomas Kurz via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am
Sa., 2. Nov. 2024, 18:47:
> when I make a record type managed by adding an `Initialize` operator
> according to [1], can I rely on the record being initialized at the
> beginning of a function in which the record is the function result?
>
> Example:
>
> type TRec = record
> // whatsoever
> class operator Initialize(var aRec: TRec);
> end;
>
> function DoSomething: TRec;
> begin
> // <---- can I assume that `Result` is initialized when the function is
> entered?
> // more code
> end;
>
> [1] https://wiki.freepascal.org/management_operators
As with any managed type result the Result variable will be initialized
*outside* of the function and passed in as a var-parameter. Depending on
the code and due to how the compiler manages temporary variables involved
in this, this can lead to your code receiving a Result variable upon entry
that had been used by a previous call.
So to be on the safe side assign a Default(YourType) to it (needs current
FPC 3.3.1 to work correctly) or use a local variable of the same type and
overwrite the Result.
Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20241103/8aea1715/attachment.htm>
More information about the fpc-pascal
mailing list