7 ms·
I meant that the intent is obviously the chained comparison. If lots of people do this (I have no idea) then it'd be nice to ensure we're doing that nicely, and
by tialaramex 4y ago
I meant that the intent is obviously the chained comparison. If lots of people do this (I have no idea) then it'd be nice to ensure we're doing that nicely, and if necessary here's syntax for expressing it (not in C though, where it already does the wrong thing).
What on Earth does your D expression do for numeric types? And why?
In Rust I can write this only if, in fact, c is always ordered with respect to a boolean which means c is itself a boolean. Since c is a boolean then ((a<b)<c) actually means ((a>=b) && c) which er... I guess? I doubt that writing either when you really meant the other will make your intent clear.
- celrod 4y ago> I meant that the intent is obviously the chained comparison. Yes, "Is `b` between `a` and `c`?" is a natural and common question to ask. FWIW, Julia does lower `a < b < c` as a chained comparison: julia> Meta.@lower a < b < c :($(Expr(:thunk, CodeInfo( @ none within `top-level scope` 1 ─ %1 = a < b └── goto #3 if not %1 2 ─ %3 = b < c └── return %3 3 ─ return false )))) That is, it turns `a < b < c` into `(a < b) && (b < c)`. You can make the chain arbitrarily (within reason) long. Of course, if a language doesn't lower chained comparisons in this way, I would much rather have a compiler error than have it silently return `(a < b) < c`.
- WalterBright 4y ago> I would much rather have a compiler error than have it silently return `(a < b) < c` Exactly.
- tialaramex 4y agoSure, we're all in agreement that C and C++ (and probably some other semi-colon languages) make the wrong choice here. In Godbolt I see "missing closing `)` after `if (a < b`" from D which unlike the Rust error doesn't acknowledge the user's intent and instead is talking about grammar. Hopefully the user will go read the documentation and find out that nope, you can't do that in D, because if they just adjust their code to silence the errors then they end up with ((a < b) < c) and that does... not do what they intended.
- WalterBright 4y agoD does it with a grammar rule, not by looking for the case after the AST is constructed. It simply won't parse. Hence the origin of that error message.
- celrod 4y agoI just tried Python, and it behaves like Julia. So I agree the two reasonable behaviors are: 1. Chain the operations like Julia/Python 2. Throw an error, ideally with an informative message.
- deleted 4y ago[deleted]