[fpc-pascal] Property write access on records

Sven Barth pascaldragon at googlemail.com
Tue Mar 3 07:55:35 CET 2020


Ryan Joseph via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am
Mo., 2. März 2020, 22:47:

> In the snippet below why isn't it an error to write to a field of the
> read-only property? I was hoping this would give me some type safety but it
> just writes to a temp variable and does nothing. Not very helpful if you
> ask me.
>
>
> =========================
>
> type
>   TSheet = record
>       m_tableSize: TVec2i;
>       function GetTableSize: TVec2i;
>       property TableSize: TVec2i read GetTableSize;
>   end;
>
> var
>   sheet: TSheet;
> begin
>   // why isn't this an error?
>   sheet.x := 1;
> end;
>

I assume you mean "sheet.TableSize.x"? Otherwise your example makes no
sense...

In that case: you are working on a temporary copy of the record. It does
not affect the TSheet instance anymore (that's essentially the same reason
why C operators or var parameters are not allowed for properties).

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20200303/e50ed6f6/attachment.html>


More information about the fpc-pascal mailing list