ChaiScript/unittests/nested_namespaces.chai
leftibot dd9afc832e Address review: use :: instead of . as nested namespace separator
Switch from dotted names (e.g. "constants.si") to C++-style ::
separator (e.g. "constants::si") for nested namespace declarations,
both in the C++ API (register_namespace) and in script (namespace()).

The original implementation used . because namespace members are
accessed via dot notation at runtime (constants.si.mu_B), making the
declaration separator match the access syntax. However, :: is more
consistent with C++ namespace conventions and aligns with ChaiScript's
existing use of :: for method (def Class::method) and attribute
(attr Class::attr) declarations.

Member access in scripts remains dot-based (constants.si.mu_B) since
that is ChaiScript's member access operator.

Requested by @lefticus in PR #675 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 15:56:38 -06:00

26 lines
579 B
ChaiScript

// Test nested namespaces using C++-style :: separator
namespace("constants::si")
constants.si.mu_B = 1.0
namespace("constants::mm")
constants.mm.mu_B = 2.0
assert_equal(1.0, constants.si.mu_B)
assert_equal(2.0, constants.mm.mu_B)
// Test deeper nesting
namespace("a::b::c")
a.b.c.val = 42
assert_equal(42, a.b.c.val)
// Test that existing namespace can gain nested children
namespace("math")
math.square = fun(x) { x * x }
namespace("math::trig")
math.trig.double_angle = fun(x) { 2.0 * x }
assert_equal(16, math.square(4))
assert_equal(6.0, math.trig.double_angle(3.0))