Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2024-11

Managed record types

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Thomas Kurz

Hello,

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

Reply by email

Bart
to Thomas Kurz

Consider this:
====
type

{ TRec }

 TRec = record
  // whatsoever
    class operator Initialize(var aRec: TRec);
  end;

{ TRec }

class operator TRec.Initialize(var aRec: TRec);
begin
  writeln('class operator TRec.Initialize(var aRec: TRec)');
end;
function foo: TRec;
begin
  writeln('foo');
end;

begin

  foo;

end.
===
It outputs:
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test class operator TRec.Initialize(var aRec: TRec)
foo

So, the initialization code is called before anything in the function.

Bart

Show quoted text 23 lines
Hello, 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 _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Bart

Reply by email

Sven Barth
to Thomas Kurz: when I make a record type managed by adding an `Initialize` operator according to [1],…

Thomas Kurz via fpc-pascal schrieb am
Sa., 2. Nov. 2024, 18:47:

Show quoted text 19 lines
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

Reply by email

Thomas Kurz
to Sven Barth

Thank you. This was a very valuable and helpful comment.

Maybe we can add this information to the wiki?

Earlier message 42 lines
----- Original Message ----- From: Sven Barth via fpc-pascal To: FPC-Pascal users discussions Sent: Sunday, November 3, 2024, 11:06:53 Subject: [fpc-pascal] Managed record types Thomas Kurz via fpc-pascal 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

Reply by email