godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
87.19k stars 19.6k forks source link

Doing =+ should raise a syntax error #31208

Open ReyAnthony opened 4 years ago

ReyAnthony commented 4 years ago

Godot version: 3.1.1 stable official

OS/device including version: OSX 10.14.4

Issue description: I made a typo, instead of writing weight += o.get_weight() I wrote weight =+ o.get_weight()

IMHO, if there is no space beetwen the assignement and the + sign, then the syntax should not be valid, to avoid such error to be time consuming

Steps to reproduce: Just test something like weight =+ o.get_weight()

Sslaxx commented 4 years ago

This code snippet results in 1 being printed on the console:

    var a = 0
    a =+ 1
    print (a)

If the code does the same thing, should this be considered a bug?

ReyAnthony commented 4 years ago

@Sslaxx Yeah, that's the same thing. I don't really consider to be a bug, it's perfectly valid to write a = +1

It's a bit weird, but it does not bother me, the problem is more about the whole =+ statement. If you force the space, then IMHO, no problem.

ReyAnthony commented 4 years ago

But I guess, it might break too much for an error that can happen every once in a while And as far as I checked, Java C# and Python are mostly happy with the same syntax.

Calinou commented 4 years ago

We could add a GDScript warning when a script contains =+/=-/=*/…. The GDScript style guide already recommends spacing operators from other symbols, so this is probably fine to enable by default.

However, I'm not sure if the GDScript warning system can catch purely syntactic mistakes (as = + and =+ translate to the same compiled code).

Xrayez commented 4 years ago

It's a bit weird

I do this:


func get_motion():
    var left  = -Input.get_action_strength("move_left")
    var right = +Input.get_action_strength("move_right")
    var up    = -Input.get_action_strength("move_up")
    var down  = +Input.get_action_strength("move_down")

    return Vector2(left + right, up + down)

And no I'm not convinced to remove those whitespaces in this case, sorry! 🙂

girng commented 4 years ago

can try += instead. no errors, valid gds code

ReyAnthony commented 4 years ago

@Calinou 👍 Nice, this avoids messing with the parser/tokenizer (or what else) code and enforces good practices :)

@Xrayez Yeah, as I said, this is perfectly valid syntax. Appending a + before any integer is 'weird', as it will not switch the sign and is thus useless.

@girng Yeah, that's the whole point, the whole issue is about me not typing this and making a typo

Xrayez commented 4 years ago

Appending a + before any integer is 'weird', as it will not switch the sign and is thus useless.

Unless we come up with operator overloading mechanism which is unlikely to happen. 😏

But yeah I see this only as a readability/self-documenting code use case, which could potentially break stuff if this kind of style is forbidden by GDScript parser in the future.

ReyAnthony commented 4 years ago

@Xrayez Yeah sure then it might mean otherwize lol As for breaking stuff, I totally agree, that's why I think @Calinou solution is a nice way to 'solve' the issue while not breaking anything.

aaronfranke commented 4 years ago

@Sslaxx The issue is when the initial value isn't zero: (btw pinging @vnen)

    var a = 5
    a =+ 1
    print (a) # Prints 1
    var b = 5
    b += 1
    print (b) # Prints 6