make (a < b < c) illegal?
Miles
_______ at _______.____
Thu Feb 8 05:51:06 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?
In C, C++ and D, the way they are now, my first thought is the
programmer needs to be replaced.
> and that it does NOT
> mean ((a < b) && (b < c)).
In a perfect language, the language should understand exactly what the
programmer meant. I would love to be able to type thinks like:
if (1 < x <= 10)...
It makes a lot of sense. Python, ruby and other languages take it correctly.
> and even if it was intentional, it raises such
> a red flag that it shouldn't be used anyway.
Someone doing this intentionally points to a bug in the D spec.
> 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)
I don't think so. Considering a, b and c of scalar type, if (a < b)
returns a boolean result, it should NOT be comparable with a scalar.
This is why I insist again that not having a true boolean type in D is a
big minus.
In my opinion:
(a < b < c) -> (a < b) && (b < c)
(a < b == c > d) -> (a < b) && (b == c) && (c > d)
((a < b) < c) -> ERROR: comparing non-scalar and scalar types
More information about the Digitalmars-d
mailing list