Search code examples
zig

How to re-export functions from a third-party module


I have a module foo.zig which is useful but I want to augment it with more functions, without modifying it, so I created foo-wrapper.zig that has one or two more functions, and foo.zig has dozens of functions.

How do I re-export (with pub or something) all functions from foo.zig to all consumers of foo-wrapper.zig?


Solution

  • just declare the function name in foo-wrapper.zig and add the pub keyword.


    foo.zig

    pub fn hello() void {
     std.debug.print("Hello", .{});
    }
    

    foo-wrapper.zig

    const foo = @import("foo.zig");
    pub const hello = foo.hello;
    // or do
    pub usingnamespace @import("foo.zig");
    
    pub fn helloWorld() void {
     hello();
     std.debug.print(" World", .{});
    }
    

    main.zig

    const foo_wrapper = @import("foo-wrapper.zig");
    
    pub fn main() void {
      foo_wrapper.helloWorld();
      foo_wrapper.hello();
    }