make (a < b < c) illegal?

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Wed Feb 7 17:06:07 PST 2007


Walter Bright wrote:
> Right now, in D (as well as C and C++), when you see the expression:
> 
>     if (a < b < c)
> 
> what is your first thought? Mine is that it was written by a newbie who 
> didn't realize that (a < b) returns true or false, and that it does NOT 
> mean ((a < b) && (b < c)). The odds approach certainty that this is a 
> logic error in the code, and even if it was intentional, it raises such 
> a red flag that it shouldn't be used anyway.
> 
> Andrei has proposed (and I agreed) that this should be done away with in 
> the language, i.e. comparison operators should no longer be associative. 
>  It's a simple change to the grammar. If one really did want to write 
> such code, it could be done with parentheses:
> 
>     if ((a < b) < c)
> 
> to get the original behavior. At least, that looks intentional.
> 
> I don't think this will break existing code that isn't already broken.

For the record, (clears throat) let me add that I initially suggested to 
make them associative with the right meaning, but was shot down. Making 
the comparison operators (and that includes != and ==) nonassociative is 
the next best thing to do.


Andrei



More information about the Digitalmars-d mailing list