I am using CodeGear Delphi 2007 for Win32,
look at the folowing code, the error is described at the end of text:
TAbstractEnumerator = class abstract
function GetCurrent: Integer; virtual; abstract;
function MoveNext: Boolean; virtual; abstract;
property Current: Integer read GetCurrent;
end;
TMyEnumerator = class(TAbstractEnumerator)
private
Index : integer;
public
function GetCurrent: Integer; override;
function MoveNext: Boolean; override;
property Current: Integer read GetCurrent;
constructor create;
end;
TAbstractList = class abstract
procedure Add; virtual; abstract;
function GetEnumerator : TAbstractEnumerator; virtual; abstract;
end;
TMyList = class(TAbstractList)
procedure Add; override;
function GetEnumerator : TAbstractEnumerator; override;
end;
constructor TMyEnumerator.create;
begin
Index := 0;
end;
function TMyEnumerator.GetCurrent: Integer;
begin
Result := Index;
end;
function TMyEnumerator.MoveNext: Boolean;
begin
Inc(Index);
Result := Index < 10;
end;
procedure TMyList.Add;
begin
inherited;
// do nothing
end;
function TMyList.GetEnumerator: TAbstractEnumerator;
begin
Result := TMyEnumerator.create;
end;
var List : TAbstractList;
i : integer;
Enumerator : TAbstractEnumerator;
begin
List := TMyList.Create;
//when I use the enumerator explicitly with while-loop, everything works fine
Enumerator := List.GetEnumerator;
while Enumerator.MoveNext do begin
Enumerator.Current;
end;
//but this code with for-in loop raises an EAbstractError - WHY???
for i in List do begin
end;
end;
look at the folowing code, the error is described at the end of text:
TAbstractEnumerator = class abstract
function GetCurrent: Integer; virtual; abstract;
function MoveNext: Boolean; virtual; abstract;
property Current: Integer read GetCurrent;
end;
TMyEnumerator = class(TAbstractEnumerator)
private
Index : integer;
public
function GetCurrent: Integer; override;
function MoveNext: Boolean; override;
property Current: Integer read GetCurrent;
constructor create;
end;
TAbstractList = class abstract
procedure Add; virtual; abstract;
function GetEnumerator : TAbstractEnumerator; virtual; abstract;
end;
TMyList = class(TAbstractList)
procedure Add; override;
function GetEnumerator : TAbstractEnumerator; override;
end;
constructor TMyEnumerator.create;
begin
Index := 0;
end;
function TMyEnumerator.GetCurrent: Integer;
begin
Result := Index;
end;
function TMyEnumerator.MoveNext: Boolean;
begin
Inc(Index);
Result := Index < 10;
end;
procedure TMyList.Add;
begin
inherited;
// do nothing
end;
function TMyList.GetEnumerator: TAbstractEnumerator;
begin
Result := TMyEnumerator.create;
end;
var List : TAbstractList;
i : integer;
Enumerator : TAbstractEnumerator;
begin
List := TMyList.Create;
//when I use the enumerator explicitly with while-loop, everything works fine
Enumerator := List.GetEnumerator;
while Enumerator.MoveNext do begin
Enumerator.Current;
end;
//but this code with for-in loop raises an EAbstractError - WHY???
for i in List do begin
end;
end;