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

Hans-Peter Diettrich DrDiettrich1 at aol.com
Sat Nov 7 12:58:43 CET 2009


Martin schrieb:

>> 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).

This *also* will happen in the above code, when the temporary variable 
is loaded or stored.

When the compiler erroneously bypassed getters and setters in the past, 
this error could have been fixed in that place, without any further 
impact on existing (compiler and application) code.

DoDi





More information about the Lazarus mailing list