godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

Allow Julia-like Short-Circuit Conditional Expressions in GDScript #10677

Open vbettaque opened 1 month ago

vbettaque commented 1 month ago

Describe the project you are working on

Feature applies to any project using GDScript.

Describe the problem or limitation you are having in your project

Feature would improve conciseness of certain GDScript expressions.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

The scientific programming language Julia allows (and encourages) the use of single-line conditional expressions by employing the short-circuiting property of || and &&. Adapting the same paradigm for GDScript would allow one to write expressions like:

<cond> and <statement>

instead of

if <cond>: 
    <statement>

and

<cond> or <statement>

instead of

if not <cond>: 
    <statement>

So instead of two lines and an indent (the if-statement can technically be written in one line, but this doesn't conform with the GDScript style guidelines) one would only have to write one line with no indent, with the additional benefit that the whole expression can be read as a sentence thanks to the and and or keywords.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

To implement this proposal, it should be sufficient for GDScript to assign a default (false) boolean value to expressions of the type void.

If this enhancement will not be used often, can it be worked around with a few lines of script?

This is a syntax addition to GDScript and therefore can't be added through script.

Is there a reason why this should be core and not an add-on in the asset library?

This addition can't be implemented by an add-on.

fire commented 1 month ago

Your proposal doesn't make a strong case how your project will benefit from this proposal because it is not specific -- every project might mean no project will benefit.

The gdscript team has team meetings regularly and it would be good to ask their opinions in dialogue though.

girng commented 1 month ago

in gdscript, we can actually do a 1 liner, like:

    if (1 != 2 and 'a' != "b" and "muffin" != "apple") or "phone" != "tablet": muffin()

func muffin():
    print("hey babe!")

i personally find

if not <cond>: 
    <statement>

easier to read. 1 liners feel more confusing and less concise (but that's just me)

Mickeon commented 1 month ago

I can't think of a good reason to be desiring this, personally, as it heavily hurts readability, which GDScript heavily prioritises.

timothyqiu commented 1 month ago

GDScript already allows making very short if statements one line.

func fact(n: int) -> int:
    if n < 0: return -1
    if n == 0: return 1
    return n * fact(n - 1)

Changing it into something like this feels alien and adds another layer of indirection when reading the code.

func fact(n: int) -> int:
    n >= 0 or return -1
    n == 0 and return 1
    return n * fact(n - 1)