Writing enumerable types

When the for..in loop was introduced in Delphi 2005, the concept of enumerable types was also introduced into the Delphi language.

As you know, there are some built-in enumerable types. However, you can create your own enumerable types using a very simple pattern.

To make your container enumerable, implement a single method called GetEnumerator, that must return a reference to an object, interface, or record, that implements the following three methods and one property (in the sample, the element to enumerate is TFoo):

function GetCurrent: TFoo;
function MoveNext: Boolean;
property Current: TFoo read GetCurrent;

There are a lot of samples related to standard enumerable types, so in this recipe you'll look at some not-so-common ...

Get Delphi Cookbook 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.