### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Point me to an error in the use of records... Please (Resolved)  (Read 320 times)

#### beria

• Jr. Member
• Posts: 70
##### Point me to an error in the use of records... Please (Resolved)
« on: September 25, 2022, 03:08:28 am »
Code: Pascal  [Select][+][-]
1. program project1;
2. type
3.
4.   TDyn = record
5.   public
6.     ItemSize: word;
7.     High: sizeInt;
8.     FCurrent: sizeInt;
9.     procedure test;
10.     class operator Initialize(var aRec: TDyn);
11.     class operator Finalize(var aRec: TDyn);
12.     function MoveNext: boolean; inline;
13.     property Current: sizeInt read FCurrent;
14.     function GetEnumerator: TDyn;
15.   end;
16.
17.   procedure TDyn.Test;
18.   begin
19.     ItemSize := 2;
20.     High := 10;
21.     writeln(' ar2.ItemSize (TDyn.Test;)', ItemSize);
22.     writeln(' ar2.High (TDyn.Test;)', High);
23.   end;
24.
25.   function TDyn.MoveNext: boolean;
26.   begin
27.     writeln(' ar2.ItemSize (TDyn.MoveNext)', ItemSize);
28.     writeln(' ar2.High (TDyn.MoveNext)', High);
29.     Inc(FCurrent, ItemSize);
30.     Exit(FCurrent < High);
31.   end;
32.
33.   class operator TDyn.Initialize(var aRec: TDyn);
34.   begin
35.     aRec.High := -1;
36.     aRec.FCurrent := -1;
37.   end;
38.
39.   class operator TDyn.Finalize(var aRec: TDyn);
40.   begin
41.   end;
42.
43.   function TDyn.GetEnumerator: TDyn;
44.   begin
45.     Result.FCurrent := -1;
46.   end;
47.
48. var
49.   ar2: TDyn;
50.   i: sizeInt;
51.
52.
53. begin
54.   ar2.Test;
55.   for i in ar2 do Write('+', i);
56.
57. 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...
« Last Edit: September 25, 2022, 06:47:10 am by beria »

#### Jorg3000

• New Member
• Posts: 41
##### Re: Point me to an error in the use of records... Please
« Reply #1 on: September 25, 2022, 06:39:12 am »
Hi!
I haven't worked with GetEnumerator before, so I'll just guess ...

Code: Pascal  [Select][+][-]
1. function TDyn.GetEnumerator: TDyn;
2.   begin
3.     Result:=self;
4.     Result.FCurrent := -1;
5.   end;
6.

#### beria

• Jr. Member
• Posts: 70
##### Re: Point me to an error in the use of records... Please
« Reply #2 on: September 25, 2022, 06:46:43 am »
Hi!
I haven't worked with GetEnumerator before, so I'll just guess ...

Code: Pascal  [Select][+][-]
1. function TDyn.GetEnumerator: TDyn;
2.   begin
3.     Result:=self;
4.     Result.FCurrent := -1;
5.   end;
6.

Question removed. You're right....
ps: Although I find this logic strange, because I've always been sure that there is always a default "self" inside the object or record itself. )))))
« Last Edit: September 25, 2022, 06:50:50 am by beria »

#### Arioch

• Sr. Member
• Posts: 414
##### Re: Point me to an error in the use of records... Please
« Reply #3 on: September 25, 2022, 12:28:54 pm »
ps: Although I find this logic strange, because I've always been sure that there is always a default "self" inside the object or record itself. )))))

There is, but it is not Self is not Result. Otherwise you won't be able to make function members in classes that have different type than class itself.

P.S. maybe could be nice if FPC/Delphi supported ^record as enumerator ducktype too, though ths would preclude automatic memory freeing, maybe not a big deal for single-threaded access