View Issue Details
|ID||Project||Category||View Status||Date Submitted||Last Update|
|0023620||FPC||Compiler||public||2013-01-09 19:41||2020-03-07 16:25|
|Reporter||Andreas Schneider||Assigned To|
|Summary||0023620: Handle with-statement to modify records in properties with setters|
|Description||The attached example demonstrates the current behavior:|
Using the with-statement on a property that directly reads/writes a record member, it successfully compiles and runs; i.e. at runtime, the values of the record fields are actually set.
When using the with-statement on a property that uses a setter, the compiler doesn't complain, but on runtime, the actual record fields stay unchanged.
At worst, this should throw a compiler error (or warning). At best, this should be handled cleanly, by calling the setter with the modified record. (Although it's probably up for discussion, whether it is clean to implicitly call methods in that case ...)
|Additional Information||I don't have Delphi at hand to test how it behaves in this particular case.|
|Tags||No tags attached.|
|Fixed in Revision|
fpcwith.lpr (1,351 bytes)
||In recent versions of Delphi, this causes a compilation error "E2064 Left side cannot be assigned to". For compatibility it is better to give an error. This is better than generating code that produces unexpected results.|
Yes the with statement should throw an error here, but it does not.
A record property only takes a full record and you can not set individual members directly.
You need to fill an intermediate variable of TTestRec and Assign that to the Test2 property. e.g:
r.a:=false;r.b:='some other text';
test2 := r;
The "Left side cannot be assigned to" error is correct (as is the behavior without with, that errors with " Argument cannot be assigned to":
tc.test2.b:='some other text';
And that is correct behavior. Both in Delphi and in Freepascal
I made a good suggestion for this for a nice compiler enhancement but I guess no one likes it...
SomeRecordProperty := With SomeRecordPorperty do Begin......End;
This would simply assign the background Record Property that WITH gets back to the Setter of the class/Record/Object etc.
All the compiler needs to do is use the already existing background Record and send it back to the setter, if it has one of course.
Just a modification of the "WITH" being able to return a value for setters or variables locally etc..
|2013-01-09 19:41||Andreas Schneider||New Issue|
|2013-01-09 19:41||Andreas Schneider||File Added: fpcwith.lpr|
|2013-01-09 19:57||Jonas Maebe||Relationship added||related to 0014534|
|2019-08-30 22:56||Jonas Maebe||Relationship added||has duplicate 0036029|
|2019-08-31 06:31||Serge Anvarov||Note Added: 0117888|
|2019-08-31 09:08||Thaddy de Koning||Note Added: 0117890|
|2019-08-31 09:08||Thaddy de Koning||Note Edited: 0117890||View Revisions|
|2019-08-31 09:11||Thaddy de Koning||Note Edited: 0117890||View Revisions|
|2020-03-07 15:54||Michael Van Canneyt||Relationship added||related to 0036768|
|2020-03-07 16:25||jamie philbrook||Note Added: 0121436|