[fpc-pascal] Forin Test

Dariusz Mazur darekm at emadar.com
Sat Nov 7 21:50:30 CET 2009


Paul Ishenin pisze:
> 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:
>
> [example]
> TMyListEnumerator = class
> public
>  function GetEnumerator: TMyListEnumeratorEnumerator;
>  function MoveNext: Boolean;
>  property Current: something;
> end;
>
> TMyList = class
> public
>  function GetEnumerator: TMyListEnumerator;
>  function GetAnotherEnumerator: TMyListAnotherEnumerator;
> end;
>
> var
>  List: TMyList;
> begin
>  for i in List.GetEnumerator do
> end;
> [/example]
>
> Loop will choose TMyListEnumeratorEnumerator, but is it desired?
>
> 'using' keyword solves this ambiguity very easy:
>
> for CollectionElement in Collection using CollectionEnumerator do
with hack with trivial operator we receive the same:
  for CollectionElement in Collection.CollectionEnumerator do

 but what about
  for CollectionElement in Collection using TreeEnumerator do
where TreeEnumerator cant traverse thought Collection

>
>>>> 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.
I can't resist.
But I have another proposition. Because we add so many variants to forin 
construction, can be added once more.
[example]
for list do ....
[/example]

where we omit in keywords and iteration variable
That will be nice solution for some  expression or in some transactional 
patterns like:
[example]
initfun;
while stopfun do begin
end;


try
  lock;
 ...
finally
 unlock;
end;
[example]



I think its not break any current behavior. Of course I can do the same 
today, but I need variable, which would not be used.
In below example we can omit declaration of i variable;
[example]
var
  i : integer;
begin
  for i in lock do begin
    procedureNotUseI;
  end;
end;
[/example]



-- 
  Darek







More information about the fpc-pascal mailing list