foreach

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 13 13:45:28 PDT 2014


On Fri, 13 Jun 2014 13:03:50 -0400, H. S. Teoh via Digitalmars-d  
<digitalmars-d at puremagic.com> wrote:

> On Fri, Jun 13, 2014 at 12:54:44PM -0400, Nick Sabalausky via  
> Digitalmars-d wrote:
>> On 6/13/2014 5:15 AM, Jonathan M Davis via Digitalmars-d wrote:
>> >
>> >I'd honestly rather see for(;;) {} removed than have foreach(; 0..n)
>> >{} added.  I don't like special cases like like these.
>> >
>>
>> Disallowing for(;;) would *be* a special case. Each of the three parts
>> can be individually omitted, and that is useful. When I only need to
>> omit one of the three (and there are times when I do), it's still a
>> cleaner more appropriate syntax than turning it into a while. So
>> making all three parts required wouldn't really work. And because of
>> that, any disallowing of for(;;) would have to be a special case.
>
> FWIW, I also see `for(;;)` as far better than `while(true)`. I like to
> read `(;;)` as "ever", so it literally reads "forever { ... }". :-) (In
> fact, in C/C++, you *could* #define ever (;;), then you can actually
> write "for ever". (Un)fortunately, in D this is not possible.)
>
> I don't like arbitrary constants like the `true` in while(true) -- it
> kinda goes against the grain, that "while" implies there is a stopping
> point, but sticking true in there contradicts this notion and is
> therefore dissonant with the concept of "while". `for(;;)`, OTOH, lines
> up very nicely with "forever". :)

#define forever while(1)

FWIW, for(;;) was preferred in C because some compilers would compile  
while(1) to literally load 1 into a register and see if it's not 0. D will  
never do this :)

-Steve


More information about the Digitalmars-d mailing list