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 and c 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
a or b or c
This returns a if a is truthy, otherwise b if b is truthy, otherwise c (regardless of whether it is truthy)
Using if statements or ternary operators for that makes the code noisier than it could be:
gdscript:
a if a else b if b else c
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
See also #1321.
We're not going to change the or operator to be return something else than boolean. That has been discussed at length and the core team is opposed to it.
See https://github.com/godotengine/godot/issues/7223 for extensive discussion on that same proposal.
Implementing null coalescing behavior as a dedicated operator, instead of hijacking or, can be considered. That's covered in #1321.
@akien-mga The proposal is about "truthy coalescing", not only null coalescing.
In some cases null coalescing would be useful, but in some others it would be not enough.
I indicated the implementation of new operator as one of the solutions to the problem.
Maybe I should rename the proposal name, or create a new one?
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
null evaluates to false when taken as a condition.
If you really wants it to be about things that convert to false, this can be discussed independently I guess but it's an even worse idea IMO. Godot has many core types whose default value evaluates to false, without it being an invalid/null value. If this suggested operator treats those as "falsy", you might have bad surprises. For example:
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
Thanks for the clarification.
Yes, I now think null coalescing could be better in general than short-circuiting.
Maybe it would be more error-proof and convenient.
I can't seem to name cases where the short-circuit I suggest would be preferable.
Maybe @KobeWi or @Sisilicon could give some arguments to have a short-circuit (||) operator [that does the same python or does] instead of / along with null coalescing (??)
As far as I know, some languages like JS have both || and ??.
GDScript already has || (alias for or) as well as && (alias for and). The difference with JavaScript is that JavaScript treats everything as an expression and by design provides return values for most anything in the grammar. Which is why logical ands and ors can also be used to "falsy-coalesce". GDScript treats logical operations as purely logical, and coerces them into booleans. It's a much more straightforward behavior but doesn't allow for shorthand assignments like you desire.
Most helpful comment
We're not going to change the
oroperator to be return something else than boolean. That has been discussed at length and the core team is opposed to it.See https://github.com/godotengine/godot/issues/7223 for extensive discussion on that same proposal.
Implementing null coalescing behavior as a dedicated operator, instead of hijacking
or, can be considered. That's covered in #1321.