Namespaces
Namespaces exist in the global scope of a CKSP program, alongside callbacks, struct definitions, and function definitions.
They contain variable declarations, function definitions and struct definitions.
Callbacks, const blocks, list blocks and families cannot be placed inside a namespace.
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.
Namespaces from an Import
A namespace does not have to be written out. import "..." as name wraps a whole file in a namespace called name, which is the common way to keep a module's declarations apart from the rest of a project.
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 variable declarations, function definitions and struct 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.