add short-circuit evaluation #10162
Closed
theDapperRaven
started this conversation in
Scripting
Replies: 1 comment 4 replies
-
Thanks for the proposal! Consolidating in #1321.
For reference, this already works in GDScript. This is also called the Elvis operator in some languages. It's like the null coalescing operator, but it falls back to the specified value whenever the value is falsy. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I usually code in python and one thing from python that I miss in GDScript (other than F strings) is its short-circuit evaluation.
If you don't know what short-circuit evaluation is, here is a python example to show it.
If
value
is truthy, thenvariable
will be defined asvalue
. If not, it will be the string "Other"In this case, None (python's null) is not truthy, so it will be considered False.
variable
ends up as "Other"What do you guys think? I think it would be a handy little feature.
Here is how it would be used in gdscpt
Nice and neat, eh?
currently, we need to do:
I suppose that it is readable, but a bit cluttered. Also a good deal longer.
One issue I saw is that currently in GDScript, the
or
keyword is seen as a comparison operator in definitions. If short-circuit evaluation is added, this functionality would need to be changed.I cant think of a use case for this. Care to enlighten me, folks?
In conclusion, I think short-circuit evaluation would make code much cleaner and more readable. I don't know how to implement this, nor do I know the engine well enough to try, so I made this a discussion.
What are your thoughts?
Beta Was this translation helpful? Give feedback.
All reactions