Open gunbuilderguy opened 1 week ago
In short, the addition would consist of modifying GDScriptParser::parse_precedence
or add to the tokenizer's get_rule
's colon token an anonymous class parser infix, getting the node's class and making a class extending it.
The problem i can see arising would be that if the value is already parsed and creates a new instance of that existing class, that you would have to assign it as being that inheriting anonymous class, and my knowledge of how that works is too limited to know if that is feasible. I at the very least think this is possible in theory.
Describe the project you are working on
I'm currently porting a java game that heavily uses anonymous classes amongst other things. I've managed to cope with things like passing from java's generous enum behaviours by making them public static constants etc.
But anonymous classes is something i cannot easily cope with without, here's a simple dummy example:
Describe the problem or limitation you are having in your project
The limitation here comes to translating this to godot while keeping it reasonably readable, as the current ways to translate it is to:
This kind of limitation becomes especially painful when you make multiple constants that are based off anonymous classes, for example; a colour database that has functions for more specific behaviors:
In the game's database, most colours don't use anonymous classes, but given there are about five hundred members, the translated version ends up with nearly a hundred script files acting as anonymous classes, and having them be inner classes would make the colour database much harder to read than it has any reason to.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
Adding an anonymous class structure in GDscript would go a long way fixing that kind of translation woe, something similar to:
equivalent to
and
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
The compiler, specifically in
GDScriptCompiler::_prepare_compilation
, handles inner classes, the code can be reused in the individual function compiling to prepare the compilation of those anonymous classes when processing those functions to treat them in a similar fashion.Currently, i'm sifting through the code and i'm working towards making a PR that would allow anonymous classes, figuring out the token system and its handlers. If this issue is just not brought up enough (a few reddit posts or godot forum posts), i'll figure out in time a working PR to implement it in godot.
If this enhancement will not be used often, can it be worked around with a few lines of script?
As specified above, it CAN be worked around but it comes at the cost of readability, and in the case of the colour database mentioned bloats the script to about twice its line count on top of hurting its readability. And as an extra inconvenience, you will have your autocomplete flooded with dozens of classes that are meant to be anonymous.
Is there a reason why this should be core and not an add-on in the asset library?
I don't think this can simply be made in an add-on at that level of code.