[fpc-devel] Published record
Michalis Kamburelis
michalis.kambi at gmail.com
Fri Aug 16 19:40:22 CEST 2024
> 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.
It is indeed a trap, and can be solved by designing a record to not
have any properties/methods that change it's state. The only allowed
changes to record state should be directly through the fields. Then
the compiler already correctly prevents you from doing
"MyClass.MyRecord.MyField:=12;" . We dealt with this in CGE too, see
https://castle-engine.io/coding_traps ,
https://github.com/castle-engine/castle-engine/blob/master/src/base/castlevectors_generic_float_record.inc#L44
.
>
> However, you can use extended RTTI to access this typeinfo and manipulate the record.
> That works.
>
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? :) This would be cool, and it is very desired by
Castle Game Engine.
To explain more our use-case in CGE:
We do use a few records (like for vectors and matrices, TVector3,
TCastleColor etc.) and we like to have properties that expose them on
object (e.g. "TCastleTransform.Translation:TVector3"). Right now,
being able to see this in the object inspector (in Lazarus or in CGE
editor) requires a bit of a hack -- we need a class, like
TCastleVector3Persistent (
https://github.com/castle-engine/castle-engine/blob/master/src/base/castlevectors_persistent.inc#L92
), that exposes the record, effectively is synchronized 2-way with
TVector3 record. And every time someone wants to expose TVector3
property to the editor, they need to
- make the TVector3 property public (and this is what should be used
by Pascal developers),
- and put the corresponding TCastleVector3Persistent in published. It
is possible, but requires some boiler-plate code each time:
https://castle-engine.io/custom_components#_publishing_vectors_and_colors
.
If we could instead just put TVector3 in the published section, and
make it exposed in the object inspector (maybe adding some additional
code to use RTTI, but that is made *once*, not every time we need to
expose TVector3) this would be great :)
Regards,
Michalis
More information about the fpc-devel
mailing list