Modules
Also known as: import package namespace export public private visibility dependency project layout toml
● Intermediate5 minBefore this: Project LayoutSpec 10
One file is one module, a folder groups modules, and nothing leaves a module unless it is marked pub.
use billing.invoice
fn main() {
const inv = invoice.create(7, 123)
print("invoice {inv.id}: {inv.total}")
}pub type Invoice {
pub id: Int
pub total: Int
note: String
}
pub fn create(id: Int, total: Int) -> Invoice {
Invoice(id: id, total: round_up(total), note: "made by billing")
}
fn round_up(x: Int) -> Int {
(x + 9) / 10 * 10
}The program ran and printed:
kavach run
invoice 7: 130src/billing/invoice.kavis the modulebilling.invoice: the path is the name, so you always know which file a name comes from.use billing.invoicebrings it in; theninvoice.create(…)calls itspubfunction.- Everything is private until marked
pub, fields included.
Private by default means you can change a module's inside freely: nothing outside can depend on what it doesn't make pub.