Using an iterator interface

If we want to write a generic version of an algorithm, one that is not bound to a data structure but to an iterator pattern, we have to somehow pass the iterator pattern to the algorithm. 

In the 2005 release, Delphi defined interfaces IEnumerable and IEnumerator. They can be found in the System unit and are defined as follows:

type  IEnumerator = interface(IInterface)    function GetCurrent: TObject;    function MoveNext: Boolean;    procedure Reset;    property Current: TObject read GetCurrent;  end;  IEnumerable = interface(IInterface)    function GetEnumerator: IEnumerator;  end;

We can write an algorithm that uses either IEnumerable or IEnumerator, but then we always have to convert the Current property to appropriate data type. ...

Get Mastering Delphi Programming: A Complete Reference Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.