This is more like a tip:
I was reviewing some code this morning by someone who was amazed his code did not work anymore.
It is the problem that the compiler has changed behaviour regards assigning member values of a record (or class for that matter) inside a for in do loop.
Well, he was right and wrong. His code used to work, but there is an easy work-around that I will add to the wiki:
program ranges;
uses
sysutils;
type
range = 1..5;
Tmyrecord = record
name:string;
end;
var
records: array[range] of TMyRecord;
num: Integer;
rec: TMyRecord;
begin
{$region 'affected code'}
{ this is illegal, because rec as an index can not be modified, not even its members. (this used to be possible, but is removed).
iow rec is immutable. }
{$if 0 this way highlighting stays..}
for rec in records do
rec.name := 'test';
{$ifend}
{$endregion}
// but this is..
for num in range do
records[num].name := num.tostring;
// and this too..
for rec in records do
writeln('record ',rec.name);
readln;
end.
This means you still have the advantage of for in do, thus ensuring correct range at all times, but does allow the assignment.
I have seen that problem before, also on the forum, but this is a correct solution.
The importance is just that it keeps the range intact at all times and you can likely not make indexing mistakes.
Btw: this is not delphi compatible, because delphi does not allow enumerating over a type.
The screenshot has an overview made by deepseek (Eat your hart out Joanna).
*"Source: DeepSeek-R1, an AI assistant by DeepSeek"*
License for the picture is:
Creative Commons CC0
The code is mine, so as usual that is without a license, with the provision you do not claim it as your own.