Description
After deliberation, I believe that the use
functionality of Zig will stay. Here is my reasoning:
This use case is valid (from https://github.com/andrewrk/tetris/blob/8c22d4956348d3ce64cdbeccc4fa8204d94a2eaa/src/c.zig):
c.zig
pub use @cImport({
@cInclude("epoxy/gl.h");
@cInclude("GLFW/glfw3.h");
@cInclude("png.h");
@cInclude("math.h");
@cInclude("stdlib.h");
@cInclude("stdio.h");
});
Usage looks like this:
const c = @import("c.zig");
test "example" {
c.printf("hi\n");
}
If pub use
wasn't possible, at the callsite it would look like this:
const c = @import("c.zig");
test "example" {
c.c.printf("hi\n");
}
Indeed, every time a file wanted to expose its own dependency to its dependents, this would happen.
If this feature existed without "splatting" into the same file, it would still be possible to obtain the previous "use" behavior. Coders could introduce an indirect file which does all the splatting, and then in the file that imports it, const x = @import("indirect.zig");
, and then when doing x.foo
we're still in the same position as before: having to look at multiple use
declarations to find the symbol.
So let's not pretend this isn't something Zig supports. Status quo use
semantics are correct. Here's my proposal to stabilize this aspect of Zig language:
- rename
use
tousingnamespace
- this is a smashedcase name, unlikely to collide with any identifiers, especially considering the style guide recommends against smashedcase anything. Frees up
use
for zig programmers to use. - it's abundantly clear what it's doing, especially if you consider that C++ has
using namespace
and it does exactly the same thing. - it's slightly long and annoying to type, which is how zig hints to programmers that maybe other options should be explored first.
- this is a smashedcase name, unlikely to collide with any identifiers, especially considering the style guide recommends against smashedcase anything. Frees up
- Previously I had avoided documenting
use
(See add docs for usingnamespace #1589) in part to discourage its use because I was not sure if it would get deleted or not. This proposal would make the feature considered relatively stable. So document it, make it clear that it should generally be avoided, and explain what are good situations for it.
One more unrelated argument for keeping it. Zig intends to beat C at its own game. Code can be translated pretty directly from C to Zig when you e.g. usingnamespace @cImport(@cInclude("stdlib.h"));
.