<div dir="auto"><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Thomas Kurz via fpc-pascal <<a href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a>> schrieb am Sa., 2. Nov. 2024, 18:47:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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?<br>
<br>
Example:<br>
<br>
type TRec = record<br>
// whatsoever<br>
class operator Initialize(var aRec: TRec);<br>
end;<br>
<br>
function DoSomething: TRec;<br>
begin<br>
// <---- can I assume that `Result` is initialized when the function is entered?<br>
// more code<br>
end;<br>
<br>
[1] <a href="https://wiki.freepascal.org/management_operators" rel="noreferrer noreferrer" target="_blank">https://wiki.freepascal.org/management_operators</a></blockquote></div></div><div dir="auto"><br></div><div dir="auto">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.</div><div dir="auto"><br></div><div dir="auto">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. </div><div dir="auto"><br></div><div dir="auto">Regards, </div><div dir="auto">Sven</div></div>