namespace
- Last UpdatedApr 24, 2024
- 1 minute read
Similarly to C++, the namespace keyword declares a scope that can contain any number of classes, methods and variables; however, in XRS Script language, namespaces are merely used as prefixes to class names. They allow you to organize code into logical groups and to prevent name collisions with other code bases.
Code example
The following example shows how to declare a namespace.
namespace Test
{
class TestClass
{
public int val_1;
public static string val_2;
private double val_3;
public static void testInstance()
{
}
}
}
Namespaces implicitly have public access and namespace members can be accessed by using the scope resolution operator (::) and the fully qualified name for each identifier, for example: Test::TestClass.testInstance().
Multiple namespaces can be declared within a .xrs file but nested namespaces are not allowed. Scope resolution operator is allowed within a namespace, making possible to have composite namespaces that resemble the nesting of namespaces: for example, namespace XR and namespace XR::Test are two different valid namespaces completely unrelated with one another, even if the latter contains the former followed by a scope resolution operator.