Support chaining comparison operators in GDScript #4004
Replies: 6 comments 7 replies
-
Almost any programming language has those problems. The syntax you have proposed there is a bit cryptic (hard to read, confusing). It could lead to decipher rather than read. Btw what about that syntax: (the optional operator is used if isn't the same of the first comparison) Examples:
That's a problem also because the parenthesis already have their role. If there's an expensive function, store its returned value in a local variable and then use it for the checks. Edited: |
Beta Was this translation helpful? Give feedback.
-
I'd prefer associating this functionality to some sort of sequence of operators or function. I'd personally love some way to incorporate a |
Beta Was this translation helpful? Give feedback.
-
Python does chaining but many other languages C, C++, JavaScript does not. Is not very common feature . I would favor only a 3 way comparison which is more common than infinite chains. Kotlin has Using Kotlin's |
Beta Was this translation helpful? Give feedback.
-
You could also open Pandora's box and rely on a robust operator precedence code that will chain any type of operators like |
Beta Was this translation helpful? Give feedback.
-
The main issue with comparison chaining is that this syntax is already accepted for bool types: true < true < false And chaining comparison operators would also change the meaning of syntax like this: a < b == c And making it behave in a different way for non-bool numbers would be inconsistent and make it unclear what a given piece of variable-using code is doing (because gdscript is not statically typed). For python, this kind of situation-specific special case is normal, but for gdscript, it's not. Lazy arrays and a way to do something like |
Beta Was this translation helpful? Give feedback.
-
I am also thinking you could use two different global functions that can be overloaded with varargs (as many variables as you want)
Might be easier than re-writing the entire GDScript operator switch logic. At present time you have to manually write these functions anyway so might as well be provided for you if multiple comparison operators will never be implemented. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What problem am I encountering?
Oftentimes I need two or more comparisons in my conditions, which makes them longer and harder to read.
Having to repeat things multiple times gets annoying with long variable names and expressions. Moreover, if you want to use a function, you have to call it multiple times.
More examples:
How chaining comparison operators would work
Here's how they would improve on the statements above:
my_var1 == my_var2 == my_var3 == my_var4
is True if these variables are all equal.-1 <= my_variable_with_a_long_name < 3
is True if the variable is in [-1, 3).0 < my_expensive_func(x) < 0.5
is True if the function returns a value in (0, 0.5). Also, you can avoid calling the function twice without the need to store its value in a variable.Only adjacent pairs are checked, so if
1 != 2 != 1
were allowed, it would return True.This feature would, in fact, be compatibility-breaking, as individual comparisons in a chain are currently treated as bools. To get this functionality back, they would need to be surrounded by brackets (which is a good practice anyway)
What to discuss?
I think the biggest point of discussion is how should chaining be allowed.
The comparison operators we have are
==
,!=
,<=
,<
,>=
,>
,in
andis
.x > y != z <= w in [42, 25, 7]
would be totally allowed and would meanx > y and y != z and z <= w and w in [42, 25, 7]
. And we can only hope no one will actually use it this way.!=
wouldn't be allowed becausea != b
andb != c
does not implya != c
and would lead to confusion. With this limitation, the operators that work together would be:<=
&<
;>=
&>
;==
;is
Are you in favor or against this idea? Is it worth the time?
Is it worth breaking a bit of compatibility for it?
Would it confuse users?
Does it interact in weird ways with other features?
Could it interact in weird ways with other proposals?
How to implement it?
Can it be worked around?
Chained comparisons can be worked around very easily, but I believe most games would use them quite often, justifying them.
#3595 This is the proposal I closed in favor of starting a discussion.
#889 This proposal discusses ranges, but not comparison chaining as a whole.
Beta Was this translation helpful? Give feedback.
All reactions