Oxeon / Examples / Modules

Modules

Modules, visibility, use imports and file modules in Oxeon.

Partly works today

Inline mod blocks, pub, pub(crate) and use (with renames, nested lists and globs) work within one file today. Float methods such as max work too. Not yet: mod util; loading another file, and std paths such as HashMap.

Modules group items and control visibility. Items are private to their module unless marked pub; pub(crate) makes an item visible to the whole mod and no further.

use brings names into scope. It accepts nested lists ({self, apply as apply_damage}), renames with as, and globs (*). pub use re-exports a name from another module.

mod util; is meant to load util.ox from the mod’s directory, as Rust loads util.rs.

08_modules.ox

examples/syntax/08_modules.ox
// Modules, visibility and imports.

mod combat {
    pub mod damage {
        pub fn apply(health: f32, amount: f32) -> f32 {
            (health - amount).max(0.0)
        }

        pub(crate) fn internal_only() {}
        fn private_helper() {}
    }

    pub use damage::apply;
}

mod util;  // loads util.ox from the mod's directory

use combat::damage::{self, apply as apply_damage};
use std::collections::{HashMap, HashSet};
use crate::combat::*;

fn uses() {
    let hp = apply_damage(100.0, 25.0);
    let again = damage::apply(hp, 5.0);
    let lookup: HashMap<String, u32> = HashMap::new();
}

util.ox

examples/syntax/util.ox
// Loaded by `mod util;` in 08_modules.ox.

pub fn clamp01(x: f32) -> f32 {
    x.max(0.0).min(1.0)
}