Namespaces
We touched on the subject of namespaces earlier when we were talking about functions. A namespace is the area that a function's or variable's name is associated with. A simple example should clarify the concept.
Let's say that I have one script named 'foo' and another one named 'bar'. Each of those scripts contains a function called 'execute'. Now let's say I've got a third script from which I want to call 'execute' from the 'bar' script. Would the following make sense:
return = execute();
(pssst. the answer is no.) How is the compiler supposed to know which version of 'execute' you meant to call? If it weren't for namespaces you simply wouldn't be allowed to name two functions the same, and that would be a real drag. Instead we need to let the compiler know which version of 'execute' we meant, like this:
return = bar.execute();
That's better, now the compiler doesn't need to read our mind; we tell it exactly what we want.
Terminology |
When we specify the script that the function resides in this is referred to as 'qualifying' the function name. An un-qualified reference (such as return = execute() from above) is referred to as an 'ambiguous' or reference. |
C++ users: messiah:script uses '.' as the scope resolution operator instead of '::'. When inside a script assume: using namespace scriptname; You cannot create your own namespaces at this time.
If you call a function that's contained in the same script you do not need to qualify the function with the name of the script, it assumes you mean the one contained in the same file. If instead you wanted to use the function with the same name outside the script you will need to qualify it. For example:
foo.msa
void execute()
{
MessageBox( "Hello", 0);
}
bar.msa
void execute()
{
MessageBox("World", 0);
}
void main()
{
foo.execute(); // calls execute from
foo.msa
execute();
// calls execute from bar.msa
bar.execute(); // also calls execute
from bar.msa
}
As you can see it is not an error to qualify a function name that resides in the same file, but it is not necessary. It is only necessary if the function being called resides outside the file.
Now you might be asking "what if I name a function 'supercalifragalistic', no one is going to name a function with the same name so do I still need to qualify a call to it from outside the script it is defined in?". Yes. Since you can never guarantee that nobody will choose the same name for a function as you. You always need to qualify a function name with the script it is defined in if you are calling it from outside that script.
To put that another way, if bar.msa from above did not contain a function called execute we would still need to type foo.execute() to call execute from the foo.msa. You have to do this because you won't know if someone else defines a function called 'execute' in their script; and if they did you would end up with an ambiguous reference if it wasn't qualified with the script name.
The same rules hold true for global variables, to access a global from some other script you must qualify the reference with the name of the script that defines it.
Note: Global variables defined with static storage are not accessible outside of the script that defines them, this is true even when the namespace has been qualified. Qualifying the name lets the compiler know "which" variable you are referring to, however just because it knows what you mean doesn't mean the variable is valid (i.e. in-scope); see Scope for details.
Converted from CHM to HTML with chm2web Pro 2.82 (unicode) |