make (a < b < c) illegal?
Nicolai Waniek
no.spam at thank.you
Wed Feb 14 11:38:24 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.
I'd like to have
if (a < b < c) ...
automatically expanding to
if ((a < b) && (b < c)) ...
and
if ((a < b) < c)
raising some exception. Well, i can't give you any serious reasons for
why, but i'm used to a < b < c from math.
I tried to find something else that would make it possible to write
something like
if (b in [a,c])
where [a,b] is a compact interval (that means a <= b <= c). But this
would let it not be distinguished from an interval's declaration as well
as you wouldn't be able to write something like:
if (b in (a,c]) // a < b <= c
Perhaps someone has a nice, good looking, easy readable solution.
More information about the Digitalmars-d
mailing list