HI Sorry, this shuld be new topic on pascal devel list, but on other side this is reply on those post.
> On Thu, Mar 26, 2009 at 12:29 PM, Alexander Klenin <[email protected]> wrote: > >>> And I assume that by "lack of iterators" you mean in-language iterators? >>> >> Of course. What else? >> > > External iterators without the need for implementing descendants. But > I must agree, it would really be nice if we can incorporate iterators > in all list/container classes. > > Would the FPC developers accept such a patch? With unit tests of > coarse. :-) It will not break anything, because it never existed > before - not in Delphi or FPC. But it would make FPC classes a lot > more user friendly. > > >> versus >> >> var >> item: TItem; >> ... >> for item in obj.Items do begin >> ... item ... item ... >> end; >> >> the latter is safer, simpler and more clear code. >> > > > var > itr: ITBStringIterator; > begin > itr := gIteratorFactory..StringIterator(MyStringList); > while MyStringIterator.HasNext do > writeln(itr.Next); // .Next does not increment the iterator. > ... > // do something else with itr.Next > end; > // itr gets free'ed automatically for us... > end; > > > Traverse through container (list, array) has many approaches. In pascal we have 3 loop. Each of them is different. FOR is fastest, may include initialization: for i:=itr.start to itr.last do but is impossible to change way of computation in runtime (i think about change of direction, count ) and is no finalization REPEAT and WHILE: here is impossible to add default initialization, but finalization yes. In loop body we can change everything. thus in my program I use for i := itr.start to itr.last do or if itr.start do repeat until itr.last; It was be nice to have in language something like this: interface tTest = class function start:boolean; function last:boolean; property iterator begin start stop last; .... implementation var t : tTest begin t.iterator do begin end; end; that will be equivalent to begin if t.start then repeat until t.last. end; but then can be test by compiler (if we use both function of pair) in this construction we can achieve flexible iterator and pattern RAII (exception handling should be added) and even invoke function with locking property locking begin lock stop unlock; t.locking do something; Of course when i think that this is valuable, I should prepare path. But alone cant do this: too much code to understand on beginning. I need assistance of core developer: where start, what use. I this its similar to generic parser (I think). I may start from test cases (of course after discussion about syntax) -- Darek _______________________________________________ Lazarus mailing list [email protected] http://www.lazarus.freepascal.org/mailman/listinfo/lazarus
