Skip to content
Xen / docs

Search documentation

English · v0.1.0-demo.2

Type to search the documentation.

    한국어
    Xen / learn v0.1.0-demo.2

    Working with multiple files

    As a program grows, keep related types and functions in separate files. Create these three files and run commands from the directory containing app.xen:

    project/
    app.xen
    shop/
    model.xen
    view.xen

    app.xen:

    module app;
    import shop.model as model;
    import shop.view as view;
    fn main() {
    let item = model.Item { name: "tea", price: 3 };
    view.show(item);
    }

    shop/model.xen:

    module shop.model;
    struct Item { name: String, price: Int }

    shop/view.xen:

    module shop.view;
    import shop.model as model;
    fn show(item: model.Item) {
    println(item.name + ": " + int_to_str(item.price));
    }
    Terminal window
    $ xen check app.xen
    $ xen run app.xen
    tea: 3

    The same files are in the modules example directory. From the repository, run xen run examples/modules/app.xen.

    import shop.model; loads shop/model.xen below the entry file’s directory. Each file declares a module matching its path. An entry file importing project modules also needs a declaration such as module app;. Mismatched names are errors.

    as model provides a short name local to the importing file. Without an alias, use the full name shop.model.Item. Indirectly imported modules are not automatically available; import each module you use directly. Choose aliases that do not conflict with variables, types, or functions.

    Bring in the standard library with use std.string; or use std.string as text;. Use import for project files and use for bundled std.* and core.* modules. Do not place project files under the reserved names std or core. A single file using only standard-library modules does not need a module declaration.

    Package manifests, visibility declarations, and module re-exports are unavailable. Avoid import cycles. Put shared types in a separate file both modules can import.