← All examples

Extern block pub

Lines in an extern.C { … } block may each say pub: here strlen is public and write stays private behind a safe wrapper, as good practice #5 says.

extern_block_pub.zg
# Lines in an `extern.C { … }` block may each say `pub` (owner, 2026-10-02: "allow mixing, but block
# overrides"): here `strlen` is public and `write` stays private behind a safe wrapper, as good
# practice #5 says. A `pub` before the block would make every line public.

bare.never [fn];

module text {
    extern.C {
        pub fn strlen ['s': ptr[uint8]] -> uint64;
        fn write ['fd': int32, 'buf': ptr[uint8], 'len': uint64] -> int64;
    }
    pub fn say [lent 'msg': str.utf8] {
        no_check let 'n' := write[1, lend 'msg', str.bytes[lend 'msg']];
    }
}

MAIN {
    let 'word' := "hello";
    no_check let 'n' := text.strlen[lend 'word'];
    std::print.useful["strlen: ", 'n', newln];
    let 'line' := "written by the wrapper" ++ newln;
    text.say[lend 'line'];
}
Output
strlen: 5
written by the wrapper