-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
Short-circuiting: Make 'a or b' return first truthy item, not boolean #1887
Comments
See also #1321. |
We're not going to change the See godotengine/godot#7223 for extensive discussion on that same proposal. Implementing null coalescing behavior as a dedicated operator, instead of hijacking |
@akien-mga The proposal is about "truthy coalescing", not only null coalescing. I indicated the implementation of new operator as one of the solutions to the problem. |
Well I don't really see the difference between "null coalescing" and "truthy coalescing" in described use case in the two proposals, it seems to be two names that aim to simplify the same situation: if a:
return a
else:
return b
If you really wants it to be about things that convert to var vec_a = Vector2(0, 0)
var vec_b = Vector2(-1, -1)
print(vec_a == false) # true
print(vec_b == false) # false
# true with boolean `or`
# Vector2(0, 0) with null-coalescing operator
# Vector2(-1, -1) with falsy-coalescing operator
print(vec_a or vec_b) |
Oh, I meant "falsy coalescing" (Short-circuit), hope this was clear from the context. @akien-mga I can't seem to name cases where the short-circuit I suggest would be preferable. As far as I know, some languages like JS have both |
GDScript already has |
Closing for now:) |
Describe the project you are working on:
gdscript editor plugins
Describe the problem or limitation you are having in your project:
When writing code, I often need to choose one truthy value from several variables.
It can be an object or a non-empty dictionary / array / string
Suppose I have variables
a
,b
andc
and I need to return/get the first non-empty value.In gdscript, I can do this using
if
statements or a ternary operator, but it will not be as concise as python or some other languages do:python
This returns
a
ifa
is truthy, otherwiseb
ifb
is truthy, otherwisec
(regardless of whether it is truthy)Using if statements or ternary operators for that makes the code noisier than it could be:
gdscript:
Describe the feature / enhancement and how it helps to overcome the problem or limitation:
Making
a or b
return first truthy value (or implementing short-circuiting some other way) would solve the problem.Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
See above.
What about the usual boolean
OR
behavior?I think it can still be useful, so this behavior can be left for the
||
operator.Or vice versa, standard
or
could be left as is, and||
used for short-circuiting behavior. Or|
could be implemented for that (so as not to break compatibility)If this enhancement will not be used often, can it be worked around with a few lines of script?:
if statements/ ternary operator.
Is there a reason why this should be core and not an add-on in the asset library?:
gdscript feature
The text was updated successfully, but these errors were encountered: