Namespace
Syntax
namespace [rec] [parent.]identifier
recfor recursive β see slide nextparentfor grouping namespaces
Content
An Fβ― namespace can only contain local types and modules
Cannot contain values or functions β
By comparison, it's the same in Cβ― with
namespacethat contains classes / enums only
What about nested namespaces?
Only happens declaratively
namespace [parent.]identifier2 namespaces declared in the same file =
not nestedbut independent
Scope
Several files can share the same namespace
Several namespaces can be declared in a single file
They will not be
nestedMay cause confusion β
β Recommendations \
Only one namespace per file, declared at the top
Organize files by grouping them in directories with the same name as the namespace
0 (root)
/
Root
1
/Lev1
Root.Lev1
2
/Lev1/Lev2
Root.Lev1.Lev2
π Recursive namespace
Extends the default unidirectional visibility: from bottom to top β Each element can see all the elements in a recursive namespace.
β οΈ Drawbacks: slow compilation, risk of circular references
β Recommendation: handy but only for very few use cases
Last updated
Was this helpful?