Saltar al contenido

18 · Módulos

Estilos de importación

Calificado, por lista o múltiples módulos: cuándo usar cada estilo.

En esta página

Zolo ofrece dos estilos principales para acceder a nombres de un módulo: el acceso calificado (lib_demo::add(...)) y el acceso importado (add(...) tras use). Cada uno tiene su lugar.

Usa el acceso importado cuando un nombre se llama con frecuencia y no hay riesgo de colisión de nombres en el ámbito. El ejemplo siguiente mezcla importación única y por lista en el mismo archivo:

add viene de use lib_demo::add; sub y get_pi llegan por la lista. Ambas formas funcionan juntas en el mismo archivo.

05-qualified-vs-imported.zolo
Playground
// Feature: qualified access vs. imported name — when to use each
// Syntax: `lib::foo()` (qualified) vs. `foo()` (after `use`)
// When to use:
//   - qualified: module is large and names are generic
//     (e.g. `time::now`, `math::sqrt`); makes origin clear
//   - imported: name is unique and used a lot; reduces noise

mod lib_demo

// Imports only `add` — uses `add(...)` directly.
use lib_demo::add

// SKIP: `use lib_demo` alone (for qualified access elsewhere in
// the file) is rejected by the loader. Pull every name explicitly.
use lib_demo::{sub, get_pi}

fn main() {
  // Imported: call directly.
  let s = add(10, 5)
  print("add direct = {s}")

  // expected: add direct = 15

  let d = sub(10, 5)
  print("sub = {d}")

  // expected: sub = 5

  print("pi = {get_pi()}")
  // expected: pi = 3.14159
}

Cuando el programa usa varios módulos, declara un mod por archivo y combina use en el estilo más claro para cada módulo:

use lib_demo::greet (única) y use lib_demo::{add, sub, VERSION} (lista) conviven sin conflicto.

06-multiple-mods.zolo
Playground
// Feature: import from more than one module in the same file
// Syntax: multiple `mod` + `use` declarations at the top
// When to use: real programs almost always combine several modules;
// declare each `mod` once at the entry-point and use them at call sites.

// Loads both files alongside: `lib_demo.zolo` and
// `multi-file/helpers.zolo` (the latter we would reference via a
// relative path embedded in the multi-file folder example). Here we
// just re-use lib_demo.

mod lib_demo

// You can mix forms (single + list). The bare `use lib_demo` form
// for qualified access is currently rejected by the loader; pull
// the constant explicitly instead.
use lib_demo::greet  // single
use lib_demo::{add, sub, VERSION}  // list

fn main() {
  greet("multi")

  // expected: Hello, multi!

  print("add = {add(2, 3)}")
  print("sub = {sub(7, 4)}")
  print("VERSION = {VERSION}")
  // expected:
  // add = 5
  // sub = 3
  // VERSION = 1.0.0
}

El ejemplo de comparación de compensaciones muestra los tres escenarios en un solo lugar — wildcard para plugins nativos, lista para módulos de usuario e importación directa para nombres usados con frecuencia:

Para módulos .zolo de usuario, prefiere la lista {a, b, c}. Reserva el wildcard ::* para plugins nativos (winit, wgpu, binary...).

08-rename-tradeoffs.zolo
Playground
// Feature: tradeoffs between `use foo::*`, list, and qualification
// Syntax: three variants — side-by-side comparison
// When to use: see the guide below for each scenario.

// === Scenario 1: many names from the same module, all from a plugin ===
// Native-plugin import -> wildcard via `use plugin` is OK and idiomatic.
//
//     use plugin winit::*
//     use plugin wgpu::*
//
// === Scenario 2: you need a few specific functions ===
// Named list -> shows exactly what is in use.
//
//     use lib_demo::{add, sub, greet}
//
// === Scenario 3: rare use, or names may collide ===
// Qualified access -> keeps origin explicit.
//
//     use lib_demo
//     ...
//     lib_demo::add(1, 2)

mod lib_demo

// Mixing strategies in a single file. Bare `use lib_demo` (for
// qualified access elsewhere) is rejected by the current loader,
// so pull the occasional name explicitly too.
use lib_demo::greet  // called several times -> direct import
use lib_demo::add  // occasional, but still imported by name

fn main() {
  // Good: direct name (frequent use).
  greet("alice")
  greet("bob")
  greet("carol")

  // expected:
  // Hello, alice!
  // Hello, bob!
  // Hello, carol!

  let s = add(2, 3)
  print("sum = {s}")
  // expected: sum = 5
}

Desafío

En el ejemplo de múltiples mods, añade un segundo mod ficticio y usa un nombre de él junto a los nombres de lib_demo. Observa cómo el compilador separa los dos orígenes.

Índice global

Encuentra tu camino por Zolo

Prueba una idea

Empieza aquí

9 resultados

9 resultados

enespt-br