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 utilizations.
Getting ready
In this recipe, you'll see a file enumerable function as it exists in other, mostly dynamic, languages. The goal is to enumerate all the rows in a text file without actually opening, reading, and closing...