Skip to content

Inline modules and granular visibility

Beyond modules in separate files, Zolo lets you declare an inline module inside the file itself with mod name { ... }. This is useful for grouping related functions without creating an extra file.

use ... as name (rename) creates a local alias — handy when the original name is long or collides with another name in scope. The visibility modifiers pub(mod) and pub(crate) allow finer control than a plain pub:

  • pub(mod) — visible to siblings within the same inline module, but invisible outside it.
  • pub(crate) — visible throughout the crate (equivalent to pub today, but the parser preserves the information for future compiler phases).

mod arith { ... } is declared inline; use std::math::{abs as absolute, sign} renames during import; pub(mod) secret_factor is usable by triple (same module) but not by external code.

09-inline-mod-use-rename-pub-scope.zolo
Playground
// Feature: inline `mod foo { ... }`, `use ... as ...` rename, granular

// visibility (`pub(crate)`, `pub(super)`, `pub(mod)`).

// See `specs/mod-namespace-innovations.html` (cherry-picks C1, C4).


use std::math::{abs as absolute, sign}

// Inline module — declared inside this file, not loaded from disk.

mod arith {
  pub fn add(a: int, b: int) -> int { a + b }
  pub fn mul(a: int, b: int) -> int { a * b }

  // pub(mod) keeps the item visible to siblings inside `arith` but

  // hidden to the outside file.

  pub(mod) fn secret_factor() -> int { 3 }

  // pub(crate) is currently equivalent to `pub` for exports — the

  // tag is preserved by the parser and printed by the formatter so

  // future resolver phases can enforce crate-level scoping.

  pub(crate) fn triple(x: int) -> int { x * secret_factor() }
}

// Bring two items in with renames; the alias becomes the local name.

fn main() {
  print("arith.add(2,3)    = {arith.add(2, 3)}")       // 5

  print("arith.mul(2,3)    = {arith.mul(2, 3)}")       // 6

  print("arith.triple(4)   = {arith.triple(4)}")       // 12 (uses secret_factor)

  print("absolute(-7)      = {absolute(-7)}")          // 7

  print("sign(-9)          = {sign(-9)}")              // -1

}

Challenge

Try calling arith.secret_factor() from main. What does the compiler say? Now change pub(mod) to pub and see the difference.

enespt-br