A namespace is a container that provides a scope for identifiers like variables, functions, and classes to prevent naming conflicts, especially in large projects or when using multiple libraries.
These namespaces can apply to entire files or allow you to divide files into different namespaces and store stuff in an organised fashion.
Write namespace followed by its name and don’t provide a block.
Let’s imagine a file named utilities.cb (the name doesn’t matter.)
namespaceUtilities
let x =10
fngreet() =>println("Hello")
You can then write using followed by the namespace name in main.cb:
usingUtilities
Utilities.greet()
print(Utilities.x)
The using directive also works for Block-Scoped namespaces.
Sometimes namespace names can get annoying to write or you may have multiple namespaces with the same name. To avoid these issues, use the as keyword to rename a namespace during import:
Most languages (including Cranberry) have a standard library which contains a lot of built in functions. These include IO, Multithreading, file system, environment, etc.
Access the Cranberry standard library by writing
usingStd
The Standard library has a lot of inner namespaces, such as
IO (terminal and colors)
Task (For multithreading and benchmarking)
Env (For executable arguments)
Math (Math related functions and constants)
FS (File system and directories)
Http (Sending requests and listening server)
Random (Pseudorandom number stuff)
JSON (Parsing and stringify-ing data to JSON)
Numerics (Internal Vector and Matrices creation functions for compatibility)