Delphi supports iterator for the "for in" in different ways:
* Dynamic arrays, static arrays, sets, strings and records have "built-in" iterators * For classes and interface it requires a method called "GetEnumerator". GetEnumerator can return a class, an interface or a record. This class, interface or record must have a member called "MoveNext" and a property called "Current". MoveNext returns a Boolean and Current returns the item. You can for example do the following with a dynamic array: type TDoubleArray = array of Double; var A : TDoubleArray; D : Double; begin A := TDoubleArray.Create(1.0, 2.1, 3.2); for D in A do Writeln(D); end; On 14/06/07, Bram Kuijvenhoven <[EMAIL PROTECTED]> wrote:
Daniël Mantione wrote: > > Op Thu, 14 Jun 2007, schreef Florian Klaempfl: > >> Graeme Geldenhuys schrieb: >>> I like the "for-in" code. >> Using the default property is clean, using count imo not. Thought I >> admit I've no idea so far to do it better. > > Well, there already is a ";default;" directive, we could add a ";counter;" > directive. Something like that would be nice, but maybe an even nicer approach would be property Items[index:Integer] read GetItem write SetItem low GetLow high GetHigh; or (to be more friendly to legacy code parsers) property Items[index:Integer] read GetItem write SetItem; low GetLow; high GetHigh; Then you can use Low(object.Items) and High(object.Items), even Low(object) if Items is the default property. If you stick with the 'Count' concept, I'd opt for property Items[index:Integer] read GetItem write SetItem; count GetCount; property Items[index:Integer] read GetItem write SetItem; count FCount; Alternatives: property Items[index:Integer] read GetItem write SetItem; length GetLength; property Items[index:Integer] read GetItem write SetItem; size GetSize; To support properties that cannot be indexed by simple ordinals, an iterator mechanism could be added, like property Items[key:string]; iterator GetIterator; where GetIterator must return e.g. an IIterator interface, which is then to be defined in Objpas or the like, much like IUnkown. Add support for all this to the RTTI and you can even use it in streaming, scripting engines etc. Regards, Bram _______________________________________________ fpc-devel maillist - [email protected] http://lists.freepascal.org/mailman/listinfo/fpc-devel
_______________________________________________ fpc-devel maillist - [email protected] http://lists.freepascal.org/mailman/listinfo/fpc-devel
