Published on

Including external c functions into the global namespace in Zig

Authors

A proposal by Andrew Kelley from a couple months ago removed the ability to import external c functions into the global namespace.

Previously the following code was possible.

// Before
// A tempting pattern to avoid typing "std" everywhere
usingnamespace @import("std");
foo();

// After
// The idiomatic way to import the standard library
const std = @import("std");
std.foo();

As explained in the pull request it is favorable to be explicit in your imports and declarations as to minimize possible confusion. While I feel like I could've used this when calling external c functions, I can understand how it serves to be more verbose in your code.

// Before
usingnamespace @cImport({
    @cInclude("math.h");
});

floor(50.0);

// After
const c = @cImport({
    @cInclude("math.h");
});

c.floor(50.0);