Module Declarations
In this chapter, we'll explore understand how modules work in mana.
Module Declarations
Every Mana source file must begin with a module declaration. This tells the compiler what module the file's contents belong to.
Basic Module Declaration
module myapp
fn main() -> void {
println("Hello from myapp!")
}The module name typically matches the file name or represents the logical grouping of the code.
The Main Module
For executable programs, use module main:
module main
fn main() -> void {
println("Program starting...")
}The main module is special—it's the entry point for your program and must contain a main() function.
Library Modules
For reusable code, use a descriptive module name:
// math.mana
module math
pub fn add(a: int, b: int) -> int {
return a + b
}
pub fn multiply(a: int, b: int) -> int {
return a * b
}Submodules
Use dot notation for hierarchical modules:
// utils/strings.mana
module utils.strings
pub fn trim(s: string) -> string {
return s.trim()
}
pub fn uppercase(s: string) -> string {
return s.to_upper()
}// utils/numbers.mana
module utils.numbers
pub fn abs(n: int) -> int {
if n < 0 {
return -n
}
return n
}Module Naming Conventions
- Use lowercase names:
module graphics - Use dots for hierarchy:
module graphics.sprites - Match the file path:
src/utils/math.mana→module utils.math
File Structure
A typical project structure:
my_project/
├── mana.toml
├── src/
│ ├── main.mana # module main
│ ├── game.mana # module game
│ └── utils/
│ ├── math.mana # module utils.math
│ └── strings.mana # module utils.strings
└── build/
Continue to Visibility and Privacy.