[fpc-devel] Published record
Michael Van Canneyt
michael at freepascal.org
Sat Aug 17 16:44:34 CEST 2024
On Fri, 16 Aug 2024, Michalis Kamburelis via fpc-devel wrote:
>> You cannot publish records, and this is not planned.
>>
>> This is because things like
>>
>> MyClass.MyRecord.MyField:=12;
>>
>> do not work as one would naively expect. It does work if you use a TPersistent
>> instead of a record (the very reason the TPersistent class was invented)
>
> Do you mean the trap when "MyField" is actually a property (with
> possible setter) and "MyClass.MyRecord.MyField:=123" effectively (may)
> modify a temporary value? I feel this is something a bit independent
> to being able to publish the records.
Partly, yes.
You need "published" when you want to stream them (in a .lfm file).
But for streaming the above behaviour is prohibitive.
When using a TPersistent, it is a class which behaves 'normally'
and you get the streaming "for free": streaming a TPersistent is not
different from a TComponent.
To be able to stream published records one would need to create an empty
record, fill it with data and then assign the record (possibly recursively)
because of the above behaviour.
Doing this correctly would simply lead too far, since basically you need
to duplicate the whole mechanism that exists for classes.
A more funddamental reason is of course that records can have variant
parts and streaming that in a correct way is impossible:
you do not know what actual data is present.
Which of the variants should then be streamed ?
> Last time I checked, the RTTI information was not generated for
> records in only the "public" section, and (as per above) the records
> could not be placed in the "published" section. Is now one of the
> above possible? :)
It is since some time, but you must explicitly enable it.
The basic TObject does not have extended RTTI enabled as it is in Delphi,
except when you compile the unicode rtl.
Enabling it in TObject doubles the binary size of any project I tested it
with, so I don't plan to enable it in the default RTL.
Michael.
More information about the fpc-devel
mailing list