4 ms·
> 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 low
by 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.