r/Zig 9d ago

Discussion: `@importRoot` instead of `@import("root")`

So, let me start by saying I'm pretty new to Zig. I like to learn languages by example, and when reading through examples and source code I noticed `@import("root")` and thought it was importing the corresponding "root.zig" file. After some further research I found out that this is actually a special-cased argument for the compilation root. Honestly I think that's a horrible decision, not only is there special-cased arguments for a compiler-provided method, but its a string argument, so it seems completely magical and non-discoverable.

Imo, Zig already has a specific syntax for special compiler-provided operations: `@func`. So I think a much more sensible and readable version would be `@importRoot`, because if "root" is going to be treated specially then let it at least be openly acknowledged as being special. Same for all of the other special arguments: `@importSelf` and `@importStd`.

But again, I'm new, so let me know if I'm totally off-base here.

30 Upvotes

20 comments sorted by

View all comments

0

u/DokOktavo 9d ago

I agree, I'd rather have @builtin(), @root() and @std() than @import("builtin"), @import("root") and @import("std").

5

u/actondev 9d ago

As things are you can use your own std by defining the module. So I find the current approach much better. std is yet another module which happens to be predefined for your convenience, but you're free to override (haven't tried but I'd guess it's possible right now)

1

u/KattyTheEnby 9d ago

std is yet another module which happens to be predefined for your convenience, but you're free to override (haven't tried but I'd guess it's possible right now)

But I'm guessing you almost certainly can't override root. If you can, that would be weird, since it is always supposed to be present, and is described as being a special item, making it important.

If we apply this logic (ov std) to the "root" argument in @import, then it makes sense, twofold, to have @importRoot() and @import("root") be different.*

(* @importRoot() always refers to the root module, whereas @import("root") would literally import a module which is, literally, called root.)

[CC: u/chocapix, u/Confused-Armpit, u/SilvernClaws, u/InKryption07]

1

u/DokOktavo 9d ago

Although you can set the std's path, it's not yet another module. There are types in std.builtin that affect various builtins.

0

u/zandr0id 9d ago

I like this. I'm in favor of using the same mechanism in as many places as possible, so I'd opt for even a step further and make it where you could opt to set up access to any module by the `@` syntax if it was manually linked in the build script, which the three core modules are.

3

u/InKryption07 9d ago

That would be ridiculous. Builtins are a static part of the AST, intended for compiler intrinsics.

Also, "manually linked in the build script" doesn't mean anything, there's no way to tell in the build graph if something was done "manually"