You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am currently developing a declarative UI system in GDScript, where UI elements are defined in code. As part of this development, I aim to introduce a null-coalescing operator to simplify handling default values for UI configurations.
Describe the problem or limitation you are having in your project
In GDScript, setting default values for UI configurations can become cumbersome, especially when dealing with potentially missing or null properties. The proposed null-coalescing operator aims to streamline this process, enhancing code readability and maintainability.
Now I want to pass some UI configs to a new UI element.
For example,
varpanel=Row.new(children,config={color: "red"})
Sometimes I do not want to write all properties in the config dictionary. But still, I want the class Row to read the config and assign default value to other properties, e.g. round = 0.
The null-coalescing operator is represented by the keywords then and elthen. It provides a concise and expressive way to assign default values based on the presence or absence of specific properties within a configuration dictionary.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Simply put, this grammar, as a syntactic sweetener, will transform into ternary expressions during gdscript parsing.
Describe the project you are working on
I am currently developing a declarative UI system in GDScript, where UI elements are defined in code. As part of this development, I aim to introduce a null-coalescing operator to simplify handling default values for UI configurations.
Describe the problem or limitation you are having in your project
In GDScript, setting default values for UI configurations can become cumbersome, especially when dealing with potentially missing or null properties. The proposed null-coalescing operator aims to streamline this process, enhancing code readability and maintainability.
Now I want to pass some UI configs to a new UI element.
For example,
Sometimes I do not want to write all properties in the config dictionary. But still, I want the class Row to read the config and assign default value to other properties, e.g. round = 0.
If no null-coalescing, it may look like this:
or like this:
It seems okay here. But it can be complex.
Now I want to get a score panel when my game ends. Using declarative UI, it may look like this:
It is confusing, but the logic is simple: I want to get the detailed score of the game, run_1 or run_2 (if no run_1).
Describe the feature / enhancement and how it helps to overcome the problem or limitation
There may be a more clear way:
The null-coalescing operator is represented by the keywords
then
andelthen
. It provides a concise and expressive way to assign default values based on the presence or absence of specific properties within a configuration dictionary.Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Simply put, this grammar, as a syntactic sweetener, will transform into ternary expressions during gdscript parsing.
If this enhancement will not be used often, can it be worked around with a few lines of script?
The implementation is very simple. It will not break the compatibility. And it is more practical.
It only needs to add two keywords. Only gdscript_tokenizer and gdscript_parser four files need to be modified, adding no more than 50 lines.
The main implementation is adding a new function in
gdscript_parser.cpp
andgdscript_parser.h
.Is there a reason why this should be core and not an add-on in the asset library?
I don't know if any add-on can modify the AST in gdscript.
Future Implementation
We can even use this feature for pipe function (in JavaScript 2024) by introducing a new keyword
prev
.Longer though, clearer.
The text was updated successfully, but these errors were encountered: