I mentioned Delphi managed types is in Delphi “Variant Records”, a few notes indicating they are special.
Managed by the RTL, you can use them to your advantage when you have record types that – when on the stack – are only partially initialized by the RTL: only the managed types are initialized.
Given there is still no support to support “Initialize” and “Finalize” operators (vote for it on QualityCentral) the best you can do was mentioned by LU RD (an alias a very smart Swedish guy Leif Unéus):
Add a dummy string member into your record. Since a string is a managed type it will be initialized to an empty string when the record comes in scope.
So when calling your Clear method, test if the dummy string is empty first. Set the string to a value when appropriate to the use logic to mark the record as initialized.
It reminded me of a managed types idea that former Delphi compiler engineer Barry Kelly once posed to me when I was struggling with the initialization state of nullable records: I wanted the default to be NULL, so I added an extra field managed field “IsInitialized” and the “IsNull” property would negate that field.
Use the Delphi compiler to your advantage (:
–jeroen
via: delphi – Guarantee initialisation of stack record – Stack Overflow.
Filed under: Delphi, Delphi XE2, Delphi XE3, Delphi XE4, Development, Software Development