program project1;
type
TDyn = record
public
ItemSize: word;
High: sizeInt;
FCurrent: sizeInt;
procedure test;
class operator Initialize(var aRec: TDyn);
class operator Finalize(var aRec: TDyn);
function MoveNext: boolean; inline;
property Current: sizeInt read FCurrent;
function GetEnumerator: TDyn;
end;
procedure TDyn.Test;
begin
ItemSize := 2;
High := 10;
writeln(' ar2.ItemSize (TDyn.Test;)', ItemSize);
writeln(' ar2.High (TDyn.Test;)', High);
end;
function TDyn.MoveNext: boolean;
begin
writeln(' ar2.ItemSize (TDyn.MoveNext)', ItemSize);
writeln(' ar2.High (TDyn.MoveNext)', High);
Inc(FCurrent, ItemSize);
Exit(FCurrent < High);
end;
class operator TDyn.Initialize(var aRec: TDyn);
begin
aRec.High := -1;
aRec.FCurrent := -1;
end;
class operator TDyn.Finalize(var aRec: TDyn);
begin
end;
function TDyn.GetEnumerator: TDyn;
begin
Result.FCurrent := -1;
end;
var
ar2: TDyn;
i: sizeInt;
begin
ar2.Test;
for i in ar2 do Write('+', i);
end.
Log:
ar2.ItemSize (TDyn.Test;)2
ar2.High (TDyn.Test;)10
ar2.ItemSize (TDyn.MoveNext)0
ar2.High (TDyn.MoveNext)-1
Here is a code snippet. So it turns out that the loop on "record" completely ignores its own context of already variables in the record, initializing itself with zero garbage and therefore is not executed in the loop from 0 to "High" with step 2.....
Or maybe I just don't get it.....
At the same time, the traditional "for to" loop works fine, but we omit it...