Skip to content

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.

1
2
3
4
5
6
7
namespace Math
    declare const PI: real := 3.14159

    function square(x: real): real
        return x * x
    end function
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.

namespace UI
    declare const HEIGHT := 100
    declare const WIDTH := 500

    function set_bg_frame(frame: int)
        set_skin_offset(frame * HEIGHT)
    end function

    namespace Layout
        function content_height(): int
            return HEIGHT-68        // unqualified access to outer member
        end function
    end namespace

    namespace Fonts

        declare types: int[] := [CONTROL_PAR_FONT_TYPE, CONTROL_PAR_FONT_TYPE_ON, CONTROL_PAR_FONT_TYPE_OFF_PRESSED, CONTROL_PAR_FONT_TYPE_ON_PRESSED, CONTROL_PAR_FONT_TYPE_OFF_HOVER, CONTROL_PAR_FONT_TYPE_ON_HOVER]

        function apply(ref ui_control, font_array: int[])
            for i, type in pairs(types)
                set_control_par(ui_control, type, font_array[i])
            end for
        end function

    end namespace
end namespace

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.

1
2
3
4
5
6
7
8
namespace UI
    function area(): int
        return WIDTH * HEIGHT   // Error: WIDTH/HEIGHT not declared yet
    end function

    declare const WIDTH:  int := 500
    declare const HEIGHT: int := 100
end namespace

Correct order:

1
2
3
4
5
6
7
8
namespace UI
    declare const WIDTH:  int := 500
    declare const HEIGHT: int := 100

    function area(): int
        return WIDTH * HEIGHT   // OK
    end function
end namespace

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.

namespace UI
    declare const THEME: string := "light"

    namespace Color
        declare const THEME: string := "dark"

        function current()
            return THEME            // "dark" (UI.Color.THEME)
        end function

        function parent()
            return UI.THEME         // "light" (explicitly qualified)
        end function
    end namespace
end namespace

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.