15.9 The enumerator operator

The enumerator operator can be used to define an enumerator for any type. It must return a class, object, or extended record that has the same signature as the IEnumerator interface. Note that this operator is not recognized by the compiler under the Delphi syntax mode.

The following code will define an enumerator for the Integer type:

Type  
  TEvenEnumerator = Class  
    FCurrent : Integer;  
    FMax : Integer;  
    Function MoveNext : Boolean;  
    Property Current : Integer Read FCurrent;  
  end;  
 
Function TEvenEnumerator.MoveNext : Boolean;  
 
begin  
  FCurrent:=FCurrent+2;  
  Result:=FCurrent<=FMax;  
end;  
 
operator enumerator(i : integer) : TEvenEnumerator;  
 
begin  
  Result:=TEvenEnumerator.Create;  
  Result.FMax:=i;  
end;  
 
var  
  I : Integer;  
  m : Integer = 4;  
 
begin  
  For I in M do  
    Writeln(i);  
end.

The loop will print all nonzero even numbers smaller or equal to the enumerable. (2 and 4 in the case of the example).

More about enumerators and the enumerator operator can be found in the section 13.2.5, page 717.