[Lazarus] Passing properties as var paramatars (was: Delphi editor clone)

Martin lazarus at mfriebe.de
Sat Nov 7 05:15:43 CET 2009


Alexander Klenin wrote:
> IMHO:
>
> 1) The change was correct in the sense that all properies should be treated
> equally, regardless of their implementation. If that means (temporarily) loss
> of features, than so be it.
>
> 2) However, there is an obvious room for improvement here:
> not only Inc/Dec, but any procedure accepting var parameter
> should be able to accept writeable property of the same type.
> (especially common example is Include/Exclude for set properties)
>
> For each property argument, the compiler should create temporary variable,
> set it by reading the property before the function call, and set
> property to the new value
> after the call.
>
> Inc(MyObj.MyIntProperty);
> =>
> var
>   temp: Integer;
> begin
>   temp := MyObj.MyIntProperty;
>   Inc(temp);
>   MyObj.MyIntProperty := temp;
> end;
>
> Of course, if the compiler determines that the property is an alias for a field,
> it is free to pass the field directly -- but this is only an optimization
I had the same ideas once too.

The answer was that properties are meant to change the value on the 
object immediately. Also a property setter can raise an exception, or 
additional actions can take place or the value can be substituted....
All this wouldn't happen with a temp variable (or at best deferred). 
Therefore the behaviour would not be according to the rules of properties

Martin






More information about the Lazarus mailing list