-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
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
GDScript parse error for const inside functions #35856
Comments
Duplicate of #23695. |
@dino- This is not a bug, it's an unimplemented feature 🙂 Adding support for immutable variables isn't trivial. Also, note that We can look at improving the error message when attempting to use |
I think the error message should be made clearer so that users understand that constants can only be defined in the outer scope. |
@WARIO-MDMA A "real" constant is evaluated at compile-time* and must be a constant expression. This means you can only assign literal expressions ( An immutable variable behaves exactly like a variable, except you're not allowed to assign it again. Typically, you must also initialize the variable immediately upon declaration (unlike a mutable variable). This is equivalent to the JavaScript const someArray = [1, 2, 3];
// Valid. We're not reassigning the variable, we're only adding a value to the underlying array.
someArray.push(4);
// Invalid (we're reassigning the variable).
someArray = [1, 2, 3, 4]; This doesn't provide as much safety as compile-time constants, but is still safer than mutable variables. Modern language practices recommend using immutable variables whenever possible, as it makes the code's intent easier to understand. *: In interpreted languages like GDScript, "compile-time" refers to the moment the script is parsed and interpreted. |
@Calinou Thank you for the explanation! If immutable variables are implemented in GDScript, the difference between them and |
This is already supported in Godot 4.x, not sure if should add support in the 3.x line as well. We can probably add a better message though. |
Godot version:
Godot 3.2
OS/device including version:
Arch Linux, x86_64
Issue description:
Constant declarations aren't being allowed inside functions. I feel like we should be encouraging people to use immutable data where possible and this is preventing that. What you see is the
misplaced: const
error in the script editor.This seems similar to #8315 with the difference being that one was in script/file scope, not function scope.
Steps to reproduce:
Define a const in any function.
The text was updated successfully, but these errors were encountered: