[fpc-pascal] Forin Test

Paul Ishenin webpirat at mail.ru
Sat Nov 7 16:52:12 CET 2009

Dariusz Mazur wrote:
> Can You give example of this:
> where
>  for i in List using GetEnumerator
> is better than
>  for i in List.GetEnumerator
Of course. for-in loop expect to have a collection not an enumerator. 
What enumerator should for-in loop to choose in the next case:

TMyListEnumerator = class
  function GetEnumerator: TMyListEnumeratorEnumerator;
  function MoveNext: Boolean;
  property Current: something;

TMyList = class
  function GetEnumerator: TMyListEnumerator;
  function GetAnotherEnumerator: TMyListAnotherEnumerator;

  List: TMyList;
  for i in List.GetEnumerator do

Loop will choose TMyListEnumeratorEnumerator, but is it desired?

'using' keyword solves this ambiguity very easy:

for CollectionElement in Collection using CollectionEnumerator do

>>> And second: You cant pass enumerator as param.
>> Maybe you understood me wrong? I proposed the 'using' keyword as an 
>> extension to the current implementation. It would not be required. If 
>> not used then it will work as now but if used then compiler will 
>> search for the given identifier and use it instead of 
>> operator/GetEnumerator method.
> this is third method,
> Should be good point to implement this (pascal always has very simple 
> semantic and very strong type checking)
> Of course you then you can use any enumerator with any collection, but 
> it will be better?
> How many errors we then invoke?
Much less.

Best regards,
Paul Ishenin.

More information about the fpc-pascal mailing list