Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-11

Changing iterator usage pattern in FCL-STL

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

leledumbo

FCL-STL from the start has different iterator usage pattern compared to existing containers (say a TSQLQuery). i.e. the common pattern is:

It := SomeContainer.Iterator;
while not It.EOF do begin
  ...
  It.Next;
end;

instead, it uses:

It := SomeContainer.Iterator;
repeat
  ...
until not It.Next;

Examining the iterator code, I can see that the latter has at least the following disadvantage:
One must add additional guard before executing repeat-until to make sure that the container has at least 1 data. Otherwise, Next in until expression will trigger EAccessViolation.

I would like to change the pattern to follow existing one. However, FCL-STL has been available for some time now that changing the pattern will break existing code. How do you think I should proceed on this one?

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Changing-iterator-usage-pattern-in-FCL-STL-tp5723038.html Sent from the Free Pascal - General mailing list .

Reply by email

Sven Barth
to leledumbo
leledumbo wrote 25 lines
FCL-STL from the start has different iterator usage pattern compared to existing containers (say a TSQLQuery). i.e. the common pattern is: It := SomeContainer.Iterator; while not It.EOF do begin ... It.Next; end; instead, it uses: It := SomeContainer.Iterator; repeat ... until not It.Next; Examining the iterator code, I can see that the latter has at least the following disadvantage: One must add additional guard before executing repeat-until to make sure that the container has at least 1 data. Otherwise, Next in until expression will trigger EAccessViolation. I would like to change the pattern to follow existing one. However, FCL-STL has been available for some time now that changing the pattern will break existing code. How do you think I should proceed on this one?

I'd say don't change. In my opinion the need to restructure one's code outweighs that disadvantage. If anything then add a for-in compatible iterator like was done for TVector.

Regards,
Sven

Reply by email

Michael Van Canneyt
to Sven Barth
Sven Barth wrote 30 lines
On 08.11.2015 19:38, leledumbo wrote: > FCL-STL from the start has different iterator usage pattern compared to > existing containers (say a TSQLQuery). i.e. the common pattern is: > > It := SomeContainer.Iterator; > while not It.EOF do begin > ... > It.Next; > end; > > instead, it uses: > > It := SomeContainer.Iterator; > repeat > ... > until not It.Next; > > Examining the iterator code, I can see that the latter has at least the > following disadvantage: > One must add additional guard before executing repeat-until to make sure > that the container has at least 1 data. Otherwise, Next in until expression > will trigger EAccessViolation. > > I would like to change the pattern to follow existing one. However, FCL-STL > has been available for some time now that changing the pattern will break > existing code. How do you think I should proceed on this one? I'd say don't change. In my opinion the need to restructure one's code outweighs that disadvantage. If anything then add a for-in compatible iterator like was done for TVector.

Enumerator, you mean ?

Michael.

Reply by email

Sven Barth
to Michael Van Canneyt
"Michael Van Canneyt" wrote 27 lines
On Sun, 8 Nov 2015, Sven Barth wrote: > On 08.11.2015 19:38, leledumbo wrote: >> >> FCL-STL from the start has different iterator usage pattern compared to >> existing containers (say a TSQLQuery). i.e. the common pattern is: >> >> It := SomeContainer.Iterator; >> while not It.EOF do begin >> ... >> It.Next; >> end; >> >> instead, it uses: >> >> It := SomeContainer.Iterator; >> repeat >> ... >> until not It.Next; >> >> Examining the iterator code, I can see that the latter has at least the >> following disadvantage: >> One must add additional guard before executing repeat-until to make sure >> that the container has at least 1 data. Otherwise, Next in until

expression

>> will trigger EAccessViolation. >> >> I would like to change the pattern to follow existing one. However,

FCL-STL

>> has been available for some time now that changing the pattern will

break

>> existing code. How do you think I should proceed on this one? > > > I'd say don't change. In my opinion the need to restructure one's code

outweighs that disadvantage. If anything then add a for-in compatible iterator like was done for TVector.

Enumerator, you mean ?

Yes. For most standard cases these should be enough.

Regards,
Sven

Reply by email

leledumbo
to Sven Barth: I'd say don't change. In my opinion the need to restructure one's code outweighs that…
I'd say don't change. In my opinion the need to restructure one's code

outweighs that disadvantage. If anything then add a for-in compatible iterator like was done for TVector.

Didn't realize that the support is already there, I'll just change to for-in loop then. Silly me :S

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Changing-iterator-usage-pattern-in-FCL-STL-tp5723038p5723042.html Sent from the Free Pascal - General mailing list .

Reply by email

Graeme Geldenhuys
to leledumbo: FCL-STL from the start has different iterator usage pattern compared to existing…
leledumbo wrote 5 lines
It := SomeContainer.Iterator; while not It.EOF do begin ... It.Next; end;

Not to be a stickler, but that is not an ideal interface either (in my eyes). Not everything is a "file", so EOF (End-of-File) was really not a good choice. eg: Iterating characters in a String, Objects in a ObjectList etc... none relate to a "file".

Years ago I implemented a stand-alone unit containing various iterators for my most used container types. It is Interface based, and the Iterator design is based on Java's Iterator design - where the current pointer is between elements. This makes it easy to use without any confusion. So the above code can be written as:

Itr := gIteratorFactory.GetIterator(SomeContainer);
while Itr.HasNext do
begin
  itm := Itr.Next;
  // do something with itm
end;

No "file" related methods, no need for extra checks for the first or last element, no need to manually free the iterator in the end. Also, all Iterators have the same interface, no matter the container.

See the "Iterator Pattern" article I wrote for more details and actual code.

http://geldenhuys.co.uk/articles/

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Marcos Douglas
to Graeme Geldenhuys
Graeme Geldenhuys wrote
Not to be a stickler, but that is not an ideal interface either (in my eyes). Not everything is a "file", so EOF (End-of-File) was really not a good choice. eg: Iterating characters in a String, Objects in a ObjectList etc... none relate to a "file".

+1

The right way is using interfaces, if we talking about OOP.

Marcos Douglas

Reply by email