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

Martin lazarus at mfriebe.de
Tue Oct 20 14:28:59 CEST 2009


Marc Weustink wrote:
>
>
>> 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.
>
> I can see a use for using iterators in a for loop, however they should 
> be declared with some keyword.
>
> Something like
>
> type
>   TListIterator = iterator(TList, init_func, next_func, check_func)
>     function init_func: Boolean;
>     function next_func: <element type>
>     function check_func: Boolean;
>   end;
>
> begin
>   for element in list using TListIterator do...
>
> IMO this is more pascal than using some interface or predefined 
> function names.
+1 

"using" seems far better than a "fixed name GetIterator procedure".

IMHO the other option is to implement the feature for ase types (e.g. 
enum, array, maybe string) and allow operator overriding for all other 
types.

Then you can specify an enum operator for classes, or even for 
simple/scalar types like integer, and define your for..in behaviour for 
them.

Martin




More information about the fpc-devel mailing list