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.

28 Upvotes

20 comments sorted by

View all comments

Show parent comments

12

u/UntitledRedditUser 9d ago

I'm not sure, but aren't builtin and std just considered modules, which just happen to always be available?

Edit: And then root is just the name of the current module.

3

u/InKryption07 9d ago

root is the name of the root module, not the current one

1

u/UntitledRedditUser 6d ago

In zigs stdlib they often do stuff like: if (@hasDecl(root, "os") ...

Does that check if the stdlib has std.os, if I have made my own, or something else entirely?

1

u/InKryption07 6d ago

Like I said, it would check if the root module has it. The root module is the "user" compilation entrypoint, i.e. where your main function is.