Imports and Modules
Importing modules
Use the import statement to bring a module into scope:
After importing, you can access the module's functions using dot notation:
Import statements should be placed at the top of the file, before any function definitions or other code.
Import aliases
Use as to give an imported module a different name:
import math as m
function main() {
console.println(m.sqrt(16).tostring()) // 4
console.println(m.pi) // 3.141592653589793
}
This is useful for shortening long module names or avoiding naming conflicts.
Selective imports
Use from ... import to import specific functions or properties from a module:
from math import sqrt, pi
function main() {
console.println(sqrt(16)) // 4
console.println(pi) // 3.141592653589793
}
You can also import everything from a module:
from math import *
function main() {
console.println(pi)
console.println(e)
console.println(sqrt(25))
}
With selective imports, the imported names are available directly without the module prefix.
Available modules
| Module | Description |
|---|---|
console |
Terminal output and user input |
math |
Mathematical functions and constants |
random |
Random number generation and selection |
json |
JSON parsing and serialization |
fs |
File system operations |
datetime |
Date and time handling |
os |
OS operations and environment |
http |
HTTP client and server |
How modules work
Modules in CColon are built-in packages provided by the runtime. Each module exposes a set of functions that you call through dot notation after importing.
You must import a module before using it:
// this will fail:
function main() {
console.println("oops")
// error: undefined variable 'console'
}
Importing other CColon files
You can import functions and classes from other .ccl files using a string path:
The path is resolved relative to the directory of the file doing the import. All top-level definitions (functions, classes) from the imported file become available in the importing file's global scope.
Each file is imported at most once, even if multiple import statements reference it.
Example
utils.ccl:
main.ccl:
Importing packages
Installed packages are automatically available as modules. After installing a package with ccolon pkg install, you can import it like any other module:
Or use selective imports:
See Package Manager for details on installing packages.