Namespace

Syntax

namespace [rec] [parent.]identifier

  • rec for recursive β†’ see slide next

  • parent for 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 namespace that contains classes / enums only

What about nested namespaces?

  • Only happens declaratively namespace [parent.]identifier

  • 2 namespaces declared in the same file = not nested but independent

Scope

  • Several files can share the same namespace

  • Several namespaces can be declared in a single file

    • They will not be nested

    • May 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

Level
Folder
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?