machdocs
Home Ecosystem Changelog

Visibility

Two declaration modifiers control how a symbol is seen: pub decides what crosses a module boundary, and ext declares a function or data binding resolved at link time. What a shared library exports is a third question, answered by #[export].

The pub modifier

pub marks a declaration as part of its module's public surface. Other modules that use this module can reference pub-marked symbols by name. A declaration without pub is file-private: only code in the same file can see it.

pub fun add(a: i64, b: i64) i64 { ret a + b; }
fun helper() { ... }            # private: only callable inside this file
pub rec Point { x: i64; y: i64; }
pub tag Status: u8 { ready; failed: u32; }
pub val MAX: i64 = 100;

The modifier applies to declarations: fun, tag, rec, uni, def, val, var, ext fun, ext val and ext var. pub before use or test is accepted and has no effect: a use stays a private import, and a test is never part of a module's public surface.

Naming a private symbol from another module is refused:

ret lib.helper();               # error: no symbol `helper` exported by `example.lib`
Note

fwd always publishes and does not take an explicit pub modifier.

External functions with ext

ext declares a function or a data binding whose definition lives in another object, as a forward reference the linker resolves. An ext fun has no body and follows the C ABI. An ext val or ext var has no initializer and no storage of its own.

#[symbol("write")]
pub ext fun libc_write(fd: i64, buf: *u8, n: i64) i64;

ext var errno: i32;

Overriding the linker name

By default the linker looks up the declared name. The #[symbol("real_name")] decorator overrides it, binding the declaration to a different external symbol.

Restriction

There are no body-less functions outside of ext fun and #[intrinsic]. Regular forward declarations do not exist, and a body-less fun of any other kind is refused with a `fun` needs a body unless it is an `ext fun` or an `intrinsic`.

What a shared library exports

A kind = "shared" artifact exports exactly the declarations marked #[export], in any project of the build, and nothing else. pub states a module's interface to other Mach modules and never exports, so a pub fun is absent from the library's export table. Every other definition, and every instance the compiler makes of a generic or pack function, is hidden. #[export] builds on pub and never replaces it:

#[export]
#[symbol("area")]
pub fun area(w: i64, h: i64) i64 { ret w * h; }

#[export] on a declaration without pub is refused with `export` needs `pub`, because a library exports only its public interface. #[symbol] chooses the linker name, not the visibility: without it the export carries the module-qualified name, and a #[symbol] name with no #[export] is still hidden. To export one instantiation of a generic, export a non-generic wrapper around it. An executable exports nothing at all.

See also