It would be cool to have a scope keyword for GDScript. It would allow you to specify a scope without using functions or control statements.
This can be done already with:
if true:
var a = 0
var a = 1
However this makes code messy and harder to read.
The keyword would work like this:
scope:
var a = 0
var a = 1
Another keyword that could be added would be pscope.
It would make all code outside of the pscope inaccessible and vice-versa.
It would look something like this:
var a = 1
pscope:
var a = 0
print('a1: %s' % a)
print('a2: %s' % a)
would print:
a1: 0
a2: 1
Simply adding indentation to statements that need another scope would also be viable, but I guess might be confusing/error-prone.
Are there compelling use cases for this? I haven't seen this used often in other languages that support it. I'm not sure if variable shadowing is a good example to use, since it can quickly become confusing.
It also has sub-class potential for getting the most out of the OO style of Godot. Aside from that, inside the Godot script editor it could be used for better code folding.
I had thought of such feature several times, when I wanted to do local operation with simple var names without forcefully extracting a function which would be difficult to make sense of. Or, conversely, to not reserve variable names for too long and break a function into blocks while keeping its enclosing context available (member vars are not a good alternative!). It's available in the C-family languages, and used in several places in the engine's code.
Maybe that could be called a block?
Also, another use case is RAII: open a scope, create a file object or load some resource: it will be freed at end of scope, rather than end of function (unless GDScript lifetimes are function-based like Python)
I never saw a thing like pscope before though, can't tell much about it.
There would be considerations about the implications outside godot codebase for this type of changes.... An example: "->" symbol is breaking GdScript syntax higlighting in github, vscode, and all gdscript parsers outside this repository. In the same way with new functionality is added new documentation, when the basics of GdScript changes, the entire word orbitating outside the languaje is inmediatly broken. I think that this is bad bussines change (not neccesary, not useful, and break syntax-higlighting for not reason). "->" symbol add functionality, "scope" word don麓t.
Just to chime in, some languages (ahem, coffeescript, ahem) do this with a do keyword:
var x = 1
do:
var y = 5
x += y
print(y) # Ha, reference error
That being said, I don't see much uses for such a keyword either, apart from deallocating resources after a part of a function is done using them, but that is probably better done by splitting said function.
Don't see a compelling use case over syntactic sugar. I've used block for variable names many times before for io and reserving this keyword would break my code and probably others', as well.
I would really like it for deallocating resources. block may not be the best name for it. I'm not saying we should scope as keyword. However I find that in a lot of cases splitting a function becomes very messy.
As @bojidar-bg mentioned CoffeeScript uses do which could work since that is already a keyword.
Python uses the with keyword for things like file streams, maybe we could borrow the syntax?
I support with syntax for temporary scope. A similar thing called using is in VB to dispose of a resource immediately after exiting scope. The with keyword there is also used for temporary scope, but is used as syntactic sugar to access members by qualifying them only with a dot, similar to how gdscript accesses superclass members. I wonder if they can be combined somehow?
well if it's in a condition it's going to be scoped already. i think having the actual condition first is more explicit and less confusing (just IMO)
Adding full with functionality would be awesome. But also making it so that you can just use with without having to specify a variable would make it worth making the statement just for scope.
One way to accomplish this in GDScript right now would be by writing if true:, but it's not ideal. If it was implemented, I would recommend not making the keyword "using", but "scope" looks good.
For reference, I use it sometimes in C# to repeat blocks of code, and C# does it like this:
{
int i = 1;
// Do something with i
}
int i = 2;
// Do something with this other i
Feature and improvement proposals for the Godot Engine are now being discussed and reviewed in a dedicated Godot Improvement Proposals (GIP) (godotengine/godot-proposals) issue tracker. The GIP tracker has a detailed issue template designed so that proposals include all the relevant information to start a productive discussion and help the community assess the validity of the proposal for the engine.
The main (godotengine/godot) tracker is now solely dedicated to bug reports and Pull Requests, enabling contributors to have a better focus on bug fixing work. Therefore, we are now closing all older feature proposals on the main issue tracker.
If you are interested in this feature proposal, please open a new proposal on the GIP tracker following the given issue template (after checking that it doesn't exist already). Be sure to reference this closed issue if it includes any relevant discussion (which you are also encouraged to summarize in the new proposal). Thanks in advance!
Most helpful comment
There would be considerations about the implications outside godot codebase for this type of changes.... An example: "->" symbol is breaking GdScript syntax higlighting in github, vscode, and all gdscript parsers outside this repository. In the same way with new functionality is added new documentation, when the basics of GdScript changes, the entire word orbitating outside the languaje is inmediatly broken. I think that this is bad bussines change (not neccesary, not useful, and break syntax-higlighting for not reason). "->" symbol add functionality, "scope" word don麓t.