godot
godot copied to clipboard
GDScript 2.0: Global enum shadowing it's class_name should show specific error message
Godot version
4.0dev
System information
Pop!_OS 22.04 LTS
Issue description
When assigning a global enum to a const variable, the compile error Assigned value for constant "<name>" isn't a constant expression. is shown.
Here there shouldn't be an error, since the enum is constant.
This only happens for global enums defined with class_name (see MRP).
EDIT: This error is happening, because the enum has the same name as the class_name. If it is renamed the errors go away. I propose adding an error message when an enum has the same name as its class_name to make it clearer for the user ,why this happens.
Steps to reproduce
- Create script
GlobalEnum.gdwith content
class_name GlobalEnum;
enum GlobalEnum{A, B};
- Create script
Test.gdwith content
func _ready():
const x = GlobalEnum.GlobalEnum.A;
- Check console and see the error
Assigned value for constant "x" isn't a constant expression..
EDIT: When renaming the enum to f.e. Values, the error doesn't happen. Everything works.
Minimal reproduction project
Have you registered GlobalEnum.gd as an autoload-script?
Have you registered
GlobalEnum.gdas an autoload-script?
As you can see in the MRP, it is not registered as an autoload. But I found the error and will update the OP to reflect that.
@Calinou I think it isn't a bug anymore, but an enhancement with the proposal to add more specific error messages, when an enum name equals the class_name it is defined in.
Is there a reason why classes can't have members with the name of the outer class? Pretty sure this works just fine in most other programming languages with nested types (random example, works in Crystal). Curious to hear what the language designers think but I'd reclassify this as a bug.
Whether it's good practice is another discussion entirely (but imo it's still sensible since we don't have global enums)
@jgillich I agree, that this is a common scripting language feature. Classifying this issue as bug is in my opinion wrong, because gdscript never has supported this, so there isn't anything "broken". This enhancement should only improve the feedback to the user. To implement the feature you are requesting, I suggest opening a seperate issue. The priority of it I think is pretty low though, because with some best practice variable naming the feature is not really necessary.
Thank you for your input!
- Fixed by #70613.
Note that this is only possible for GDScript enums, not for native and @GlobalScope enums.