program tforinUpDown;

// test operator Enumerator support for classes

{$mode objfpc}{$H+}
{$APPTYPE CONSOLE}

type
  { TMyListEnumerator }
  tEnumerator=class
  protected
    FCurrent: Integer;
  public
    function MoveNext: Boolean;virtual;abstract;
    property Current: Integer read FCurrent;
  end;

  TUpListEnumerator = class(tEnumerator)
  public
    constructor Create;
    function MoveNext: Boolean;override;
  end;
   TDownListEnumerator = class(tEnumerator)
  public
    constructor Create;
    function MoveNext: Boolean;override;
  end;

  TMyList = class
     function up : tEnumerator;
     function down : tEnumerator;
  end;


function tMyList.up:tEnumerator;
begin
  result:=tUpListEnumerator.create;
end;
function tMyList.down:tEnumerator;
begin
  result:=tDownListEnumerator.create;
end;


{ TMyListEnumerator }

constructor TUpListEnumerator.Create;
begin
  FCurrent := 0;
end;

function TUpListEnumerator.MoveNext: Boolean;
begin
  inc(FCurrent);
  Result := FCurrent <= 3;
end;

{
function tEnumerator.MoveNext:boolean;
begin
  result:=false;
end;
}


{ TDownListEnumerator }

constructor TDownListEnumerator.Create;
begin
  FCurrent := 3;
end;

function TDownListEnumerator.MoveNext: Boolean;
begin
  dec(FCurrent);
  Result := FCurrent >= 0;
end;


operator enumerator (AEnumerator: TEnumerator): TEnumerator;
begin
  Result := AEnumerator
end;


var
  List: TMyList;
  i: integer;
begin
  List := TMyList.Create;
  for i in List.up do
    WriteLn(i);
  for i in List.down do
    WriteLn(i);
  List.Free;
end.

