godot icon indicating copy to clipboard operation
godot copied to clipboard

GDScript 2.0: Global enum shadowing it's class_name should show specific error message

Open AntonioDell opened this issue 3 years ago • 5 comments
trafficstars

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

  1. Create script GlobalEnum.gd with content
class_name GlobalEnum; 
enum GlobalEnum{A, B};
  1. Create script Test.gd with content
func _ready():
  const x = GlobalEnum.GlobalEnum.A;
  1. 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

63942_Test.zip

AntonioDell avatar Aug 05 '22 07:08 AntonioDell

Have you registered GlobalEnum.gd as an autoload-script?

heppocogne avatar Aug 05 '22 09:08 heppocogne

Have you registered GlobalEnum.gd as 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.

AntonioDell avatar Aug 05 '22 09:08 AntonioDell

@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.

AntonioDell avatar Aug 05 '22 09:08 AntonioDell

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 avatar Aug 05 '22 14:08 jgillich

@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!

AntonioDell avatar Aug 07 '22 08:08 AntonioDell

  • Fixed by #70613.

Note that this is only possible for GDScript enums, not for native and @GlobalScope enums.

dalexeev avatar Jun 19 '23 12:06 dalexeev