for in D versus C and C++

Steve Teale steve.teale at britseyeview.com
Thu Mar 19 11:16:25 PDT 2009


Walter Bright Wrote:

> BCS wrote:
> >> Thu, 19 Mar 2009 06:35:37 -0400, Steve Teale wrote:
> >>
> >>> for (; a<b; a++);
> >>>
> >>> is illegal in D.
> >>>
> >>> Doesn't this break a lot of C and C++ code?
> >>>
> >> for (; a<b; a++) {}
> >>
> >> is legal.  I don't think that an empty statement after for is used in
> >> "a lot of code."
> >>
> > 
> > it's a trivial fix and easy to find. Heck, you hardly need to think!
> 
> No, it isn't easy to find. This is in D because a colleague of mine, who 
> was an expert C programmer (the best in the company I was working for), 
> came to me with:
> 
> for (xxx; i < 10; i++);
> {
>       ... code ...
> }
> 
> and said he could not figure out why his loop executed only and exactly 
> once. He'd fiddled with it for a whole afternoon. He said he must be 
> missing something obvious. I said you've got an extra ; after the ). He 
> smacked his head and about fell over backwards.
> 
> So it's illegal in D, along with:
> 
>     if (condition);
> 
> and similar constructs. Have to use a { } to indicate a blank statement.

I just find this scary, because although it says at the top of the page "most of this will be familiar to C/C++ programmers" this little fact means I have to go through all of the familiar constructs with a fine tooth comb.

Also, nobody is answering the question. I can work out the alternatives, but I'd just prefer it if existing code worked. I used to use the idiom I quote all the time.





More information about the Digitalmars-d mailing list