Namespaces
Namespaces exist in the global scope of a CKSP program, alongside callbacks, struct definitions, and function definitions. They serve exclusively to contain variable declarations and function definitions. Namespaces cannot contain callbacks or struct definitions themselves.
Namespaces allow you to organize code into logical modules and prevent naming conflicts between different parts of a program. They are introduced with the namespace keyword and must be terminated with end namespace.
Inside the namespace Math, the constant is internally represented as Math.PI and the function as Math.square.
Nested Namespaces
Namespaces can be nested. In this case, inner namespaces can access members from their outer namespaces without explicit qualification.
After desugaring, members are fully qualified (e.g., UI.HEIGHT, UI.Layout.content_height()).
Partial Qualification Across Siblings
Inside UI.Fonts, a reference like Color.RED would be treated as partially qualified and completed to UI.Color.RED.
Sequential Declaration Order
Variables and constants in namespaces are resolved in program order. Only names declared above a usage point are visible. Forward references inside the same namespace are not allowed.
Correct order:
Shadowing and Scope
Inner namespaces may redeclare names from outer namespaces. Unqualified references always bind to the innermost declaration. To refer to the shadowed outer name, explicit qualification must be used.
Key Features of Namespaces
-
Global Scope Placement: Namespaces can only exist in the global scope, alongside callbacks, structs, and functions. They may contain only variable declarations and function definitions.
-
Hierarchical Prefixing: Members are internally rewritten to fully qualified names (e.g.,
UI.Fonts.apply). -
Lexical Lookup: Unqualified names are resolved from the innermost namespace outward, following lexical order.
-
Explicit and Partial Qualification: Outer and sibling namespace members can be accessed with qualified names, which are completed by the compiler if necessary.
-
Sequential Order: Declarations must appear before their use. Forward references are not allowed.
-
Shadowing Rules: Inner declarations override outer ones for unqualified references. Explicit qualification is required to access shadowed members.