Maybe we don't need foreach ;-)

Walter Bright newshound at digitalmars.com
Mon Oct 30 18:59:46 PST 2006


Bill Baxter wrote:
> Just like some people have said, you can do everything with 'for'.
> Well, actually maybe you can.  It occurred to me that foreach and for 
> really don't clash.  One requires two semicolons, the other only one. So 
> instead of a foreach we could have had
> 
>   for(a; aggregate)
>   {
> 
>   }

That looks like a syntax error for one used to for loops.

> or if the keyword 'in' had been used for the new construct instead of 
> borrowing from c's ; syntax it would be:
> 
>   for(a in aggregate)
>   {
>      ...
>   }

'in' is already an operator, so that wouldn't work.

> which could quite peacefully coexist with good-ole
> 
>   for (int i=0; i<10; i++)
>   {
>   }
> 
> Oh well.  Too much momentum behind foreach and foreach_reverse now, I 
> suppose, to make such a simplification.

foreach is a fairly well understood term, I think it needs less 
explanation than a variant on for.



More information about the Digitalmars-d mailing list