-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: chainable comparison operators #370
Comments
I think this is one of those features that looks and sounds nice, but ultimately makes the language bigger, requires the programmer to remember more things, and isn't worth that cost.
|
sounds good. |
At the moment its undecided whether zig
which I think is elegant and more intuitive then using a switch statement. You can do this now with compound
This feature only needs a tiny change to the grammar of the language, and I think its worth it:
Of course there is the argument that it can be used to write confusing expressions, but in practice people won't unless they are deliberately trying to obfuscate their code. |
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: