Skip to content
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

Closed
me2beats opened this issue Nov 24, 2020 · 7 comments
Closed

Comments

@me2beats
Copy link

me2beats commented Nov 24, 2020

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

@Calinou
Copy link
Member

Calinou commented Nov 24, 2020

See also #1321.

@akien-mga
Copy link
Member

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 godotengine/godot#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.

@me2beats
Copy link
Author

@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?

@akien-mga
Copy link
Member

akien-mga commented Nov 25, 2020

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)

@akien-mga akien-mga reopened this Nov 25, 2020
@me2beats
Copy link
Author

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 ??.

@YuriSizov
Copy link
Contributor

YuriSizov commented Nov 26, 2020

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.

@me2beats
Copy link
Author

me2beats commented Dec 9, 2020

Closing for now:)
I believe implementing a null-coalescing operator could be a better option

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants