Nested scopes
Scopes can be nested, don't confuse this statement with "libraries can be nested", in fact, you cannot even place a library
inside an scope
definition. You can however, have scope
inside either library
or scope
definitions.
An scope inside another scope is considered a child scope. A child scope is considered to be a public member of the parent scope (?).
A child scope cannot declare members that were previously declared as private or global by a parent scope.
A child scope behaves in relation to its parent in the same way as a normal scope behaves in relation to the whole map script.
Since child scopes are always public members, you can access a child scope' public members froum outside the parent scope, but it needs a prefix for the parent and a prefix for the child.
An example :
The next example will cause a syntax error:
It is actually caused by a limitation in the parser, there is a conflict caused by using N
for the parent and then declaring it for the child. However this version does not cause syntax errors:
It does kind of the same thing, but since the child's N
was declared before the parent's the parser no longer gets in a confusion.
Another thing to keep in mind is that unlike normal global variables, private/public global variables cannot be used before declared, otherwise JassHelper will think they are just normal variables.
Scopes cannot be redeclared, there cannot be 2 scopes with the same name. But 2 child scopes might have the same name if they are children of different scopes, the reason is that they actually don't have the same name, they have different names given by their public child scope situation.
There is no nesting limit, but notice that resulting variable and function names of private/public members get big and bigger depending of the depth of the scope nesting. Bigger variable names may affect performance. Not a lot but they do, and this efficiency issue is to be prevented by an obfuscator/finalizer that renames every identifier in the map script a.k.a the map optimizer's shortest names possible method.