IssueRaiser

Implement a null-coalescing operator (`??`)

Supersedes https://github.com/godotengine/godot/issues/7223 CC @Zylann **Describe the project you are working on:** This can work on any project, but currently I want to implement a "Fallback" color if the given color is null. **Describe the problem or limitation you are having in your project:** The ternary operator can be used as a fallback, but it is... - Superfluous: `a if a else b` rather than `a else b`. - Dangerous for non-const functions: `a() if a() else b()` calls `a()` twice. - Inefficient for high load functions: `http_get() if http_get() else cached_value` (Edit: Specific example doesn't make sense here) **Describe the feature / enhancement and how it helps to overcome the problem or limitation:** The enhancement would be to implement an operator that simplifies this workflow. See the code example below. **Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:** To avoid ambiguity from boolean `or`, I propose operator `else` with alternate symbol counterpart `??`. ```py func set_color(color: Color): modulate = color else Color.white ``` **If this enhancement will not be used often, can it be worked around with a few lines of script?:** This will be used often. Currently, the workaround for slow or non-const function is: ```py var http_result = http_get() var result = http_result if http_result else cached_value ``` **Is there a reason why this should be core and not an add-on in the asset library?:** It would not be poss

Posted by nathanfranke on Aug 6, 2020, 10:13 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments

github (Software)

Implement a null-coalescing operator (`??`)

nathanfrankeAug 6, 2020, 10:13 PM

Supersedes https://github.com/godotengine/godot/issues/7223 CC @Zylann **Describe the project you are working on:** This can work on any project, but currently I want to implement a "Fallback" color if the given color is null. **Describe the problem or limitation you are having in your project:** The ternary operator can be used as a fallback, but it is... - Superfluous: `a if a else b` rather than `a else b`. - Dangerous for non-const functions: `a() if a() else b()` calls `a()` twice. - Inefficient for high load functions: `http_get() if http_get() else cached_value` (Edit: Specific example doesn't make sense here) **Describe the feature / enhancement and how it helps to overcome the problem or limitation:** The enhancement would be to implement an operator that simplifies this workflow. See the code example below. **Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:** To avoid ambiguity from boolean `or`, I propose operator `else` with alternate symbol counterpart `??`. ```py func set_color(color: Color): modulate = color else Color.white ``` **If this enhancement will not be used often, can it be worked around with a few lines of script?:** This will be used often. Currently, the workaround for slow or non-const function is: ```py var http_result = http_get() var result = http_result if http_result else cached_value ``` **Is there a reason why this should be core and not an add-on in the asset library?:** It would not be poss

Comments (0)

No comments yet. Start the discussion.

Comment on this issue