proposal: chainable comparison operators #370
Labels
enhancement
Solving this issue will likely involve adding new logic or components to the codebase.
proposal
This issue suggests modifications. If it also has the "accepted" label then it is planned.
We certainly don't need this feature, but as I'm working on #114, I realized that it makes sense to be able to chain some comparison operators. C doesn't allow this, but we could do it and it would make perfect sense, so I'm writing this proposal to formally discuss the idea.
==
.a == b == c
is equivalent toa == b and b == c
(andb
is evaluated only once).<
and<=
.a OP b OP c
is equivalent toa OP b and b OP c
, where OP is one of<
or<=
.>
and>=
. similar to above.Python has this feature, but I'm proposing a more restrictive version than python's operator chaining. In python, you can do
a != b != c
, and I don't remember exactly what that does. That's a problem, so I'm proposing you can't do that in Zig. The proposal is that you can chain operators that work transitively, which is whena OP b and b OP c
then it's obvious thata OP c
. This is true for the above three cases, and isn't true of all any other mixture of comparison operators.There's one subtle issue here, which is the hidden use of the short-circuit
and
operator. It may seem at first glance thata() == b() == c()
would call all three functions when really it only needs to call the first two sometimes. That might be obvious to some readers, but might not be to all readers.Fwiw, python has the hidden short circuit operator like I'm proposing.
The text was updated successfully, but these errors were encountered: