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

Add warnings for using ++ and -- (standalone expressions that look like increment/decrement operators) #8829

Open
JeckDev opened this issue Jan 7, 2024 · 2 comments

Comments

@JeckDev
Copy link

JeckDev commented Jan 7, 2024

Describe the project you are working on

A multiplayer adventure game

Describe the problem or limitation you are having in your project

Coming to GDScript from embedded C and C++, the unary ++ and -- operators are nearly instinctual to me.

Now, #2869 lays out pretty clearly that adding these operators isn't planned. However, the current state of GDScript allows a line such as:

var idx : int = 0
++idx

This will compile and run without warning, but will leave the variable unmodified. It's not hard to imagine a situation in which this introduces subtle and hard to track down bugs, because the coder assumes these operators work and nothing in the editor says otherwise.

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

The simplest solution I can imagine is to add a warning when "++" or "--" are encountered.

This notifies the programmer that their assumption about these operators behavior is false.

As far as I know, there's no valid reason to write "++" or "--" besides intending on an increment/decrement, so warning that this doesn't work wouldn't interfere with existing behavior.

(I guess technically someone could write --1 and mean "positive 1", but in this case the warning could be ignored, and besides who does that?)

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

I haven't dived into the code yet, but an easy implementation would be to scan a line for "++" or "--" directly and produce the warning then. More ambiguous parses like "+ +" or "- -" can be left alone.

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

Obviously this isn't hard to work around. Programmers can just read the docs and see that unary increment/decrement isn't supported.

However, I think that as more people come to Godot from other game engines and other languages, this is a minor change which prevents a minor issue.

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

As part of GDScript, obviously this should be core.

@Mickeon
Copy link

Mickeon commented Jan 8, 2024

While I do agree, to be pedantic:

This will compile and run without warning

This is not true.
image

@Calinou Calinou changed the title Add warnings for ++ and -- Add warnings for using ++ and -- (standalone expressions that look like increment/decrement operators) Jan 8, 2024
@KillbotGamma
Copy link

It looks like there is a warning when using ++ with an int literal, but not when using it with a variable
image

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

4 participants