-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
GDScript: support mixing multiple variable definitions and expressions in if-statement #98335
base: master
Are you sure you want to change the base?
Conversation
bfde7e1
to
c035f99
Compare
f55af4d
to
590a8cb
Compare
I'm not sure about the comma syntax, it's basically a weird |
Both the comma syntax and multiple definitions / conditions are borrowed from Swift (optional binding). As a Swift user, the comma syntax appears more readable than As for multiple definitions / conditions, it is mainly for static typing. While GDScript is a dynamic typing language, some people like me actually use it with static typing as much as possible. Static typing will introduce extra code. For example, consider dragging: Without static typing:
Is very readable. But if one write it with static typing (maybe for performance and static checking):
If only one definition / condition is allowed, the first With static typing and multiple conditions:
This one requires less code lines and is as readable as the first dynamic typing one. Multiple conditions is useful for code with static typing. Code with dynamic typing perhaps do not need it at all. |
Still, the comma syntax should be a separate proposal and separate PR IMO. Can you split it? |
Sure. I close this PR for now and will reopen it when split is done. |
The reason of using comma here is that
To fix it:
The one works but requires additional parentheses. With comma syntax, it is simple:
|
UPDATE: As @KoBeWi suggested, the original PR is split into two commits. The first one implements support of variable definition in if-statement (godotengine/godot-proposals#2727). The second one introduces a new syntax for multiple variables and conditions in if-statement. For the rationale behind the second commit, please see comment and comment below.
Another attempt to implement godotengine/godot-proposals#2727 for if-statement. This patch implements two functions: allow variable definition as condition (if var n := foo():
) and allow multiple conditions separated by comma in a single if-statement(if foo(), var n := bar(), n > 1:
).About implementation of the second commit: the
ExpressionNode *condition
field ofIfNode
is replaced with a list of eitherExpressionNode
orVariableNode
. When parsing a single if-statement, a temporary block(SuiteNode
) is created for parsing multiple conditions within and then the true block within. For an expression condition, oneExpressionNode
is created. For a variable definition, oneVariableNode
and oneExpressionNode
are created. The former one is used for generating code for initial assignment while the later one is used for testing as usual.Compilation is done like usual except that, all of the not-true-jump-address for each condition will be patched to the same starting address else/elif if any, otherwise the end address of the if statement. Thus, the
write_else()
interface is changed towrite_else(int count)
.