[fpc-devel] New feature discussion: for-in loop

Michael Van Canneyt michael at freepascal.org
Tue Oct 20 10:34:43 CEST 2009



On Tue, 20 Oct 2009, Paul Ishenin wrote:

> Michael Van Canneyt wrote:
>
>> At least one of the reasons we never did implement for-in is the absolutely
>> horrible and totally wrong idea to use classes/interfaces for this, to 
>> which
>> I seriously objected.
>
> Reading this I conclude you are against 'for-in' implementation in fpc.

No.

>
>> I can understand
>>   for y in enumerated do
>> or
>>   For y in set do
>> or
>>   For Y in range do
>> Because this seems a natural extension of loops to enumerates (and even
>> then) But not ever the class-based one.
>
> So you are not against it but you want to allow them only for the base types?

Yes.

>
> I hope you understand that ppl will use them for classes anyway?

Those people should be re-educated to use iterators.

>
> var
>  P: Pointer;
> begin
>  for P in GetComponents(Component) do
>    TComponent(P).DoSomething;
> end;
>
> And what is so special in the class type?

First:
For a for loop, you are guaranteed that the loop logic behaves correctly.
A loop is equivalent to the following:

I:=StartValue;
E:=EndValue;
While I<=EndValue do
   begin
   // loop stuff
   Inc(I);
   end;

And you cannot change I manually during the loop.

You don't know this with an iterator since you depend on the implementation
of the iterator. The loop could loop forever then...

Secondly:
You promote a certain class/interface to a language feature. 
The compiler then depends on the presence of a certain class 
with some 'known' methods in the RTL.

Both points are simply wrong from a language point of view.

It is bad enough that the second point is already so for interfaces and even 
TObject, (a very serious design flaw by Borland) but extending this even further 
to include actual language features such as the for loop is 2 bridges too far
as far as I'm concerned.

Especially when you can have perfectly the same functionality with iterators.

Borland could have perfectly satisfied people by implementing a set of iterators 
in the classes unit. Instead they raped - yet again - the pascal language.
(they are a commercial company, so what else can we expect...)

Michael.



More information about the fpc-devel mailing list