Proyecto multi-archivo
En esta página
Cuando un proyecto crece, el código se divide en varios archivos. En Zolo, cada
archivo es un módulo: el archivo de entrada declara los módulos con mod e
importa lo que necesita con use. El compilador localiza cada archivo por
nombre, en el mismo directorio que el archivo que declara el mod.
El siguiente ejemplo muestra un programa con tres archivos: main.zolo (punto
de entrada), greeter.zolo (módulo de saludo) y helpers.zolo (utilidades
numéricas). Como los archivos dependen entre sí, deben ejecutarse juntos — no
es posible correrlos de forma aislada en el playground del navegador.
helpers.zolo — define funciones numéricas públicas y una constante privada:
Módulo de soporte: add, double y circle_area son públicos; PI es privado
al módulo (sin pub).
// Module: helpers — numeric utilities.
pub fn add(a: int, b: int) -> int {
return a + b
}
pub fn double(x: int) -> int {
return x * 2
}
const PI: float = 3.14159
pub fn circle_area(r: float) -> float {
return PI * r * r
}
Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.
greeter.zolo — expone hello y farewell; la función auxiliar
build_prefix permanece privada:
build_prefix no tiene pub — es un detalle de implementación invisible para
main.zolo.
// Module: greeter
// Anything `pub` is exported; the rest is private to the module.
pub fn hello(name: str) {
let prefix = build_prefix()
print("{prefix}, {name}!")
}
pub fn farewell(name: str) {
print("Bye, {name}.")
}
// Private — called only from inside the module. It is not exposed
// in the `use greeter::...` over in main.
fn build_prefix() -> str {
return "Hello"
}
Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.
main.zolo — punto de entrada que orquesta los dos módulos. El self en
la lista de use helpers trae el namespace helpers al ámbito al mismo tiempo
que los nombres individuales:
use helpers::{self, add, double, circle_area} permite tanto add(2, 3)
(directo) como helpers.add(10, 20) (calificado vía namespace). Para ejecutar:
zolo run main.zolo dentro de la carpeta multi-file/.
// Feature: multi-file program with `mod` + `use`
// Structure:
// main.zolo <- entry point
// greeter.zolo <- `greeter` module
// helpers.zolo <- `helpers` module
//
// To run (from this folder):
// zolo run main.zolo
// Declares that we will use the `greeter.zolo` and `helpers.zolo`
// files as modules. `mod` resolves each one by reading the file
// from the same directory.
mod greeter
mod helpers
// Imports specific names via `use`. The `self` entry in the list brings
// the module table itself into scope alongside the destructured items,
// so both `helpers.add(...)` and the bare `add(...)` work.
use greeter::{hello, farewell}
use helpers::{self, add, double, circle_area}
fn main() {
hello("zolo")
// expected: Hello, zolo!
let sum = add(2, 3)
print("2 + 3 = {sum}")
// expected: 2 + 3 = 5
// `helpers` is bound thanks to the `self` entry in the use list.
print("helpers.add(10, 20) = {helpers.add(10, 20)}")
// expected: helpers.add(10, 20) = 30
print("double of 7 = {double(7)}")
// expected: double of 7 = 14
print("area(r=2) = {circle_area(2.0)}")
// expected: area(r=2) = 12.56636
farewell("zolo")
// expected: Bye, zolo.
}
Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.
Consulta también