-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Special name for the current package: @import("self")
#6279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Accepting now as I think this will be uncontroversial and I don't really see any downsides. One interesting thing to note is that for applications, |
May i add a follow-up proposal that could be included here: As const network = @import("network");
const std = @import(.std); This would make clear that these packages are magic and provided by the compiler whereas every string is to be given on the command line or in |
@MasterQ32: I don't think this can be done with |
@xackus Builtins (as in functions) can accept any type. In this case: A enum literal, not value from a explicit. This means it should work... But some of the compiler/language devs should think about it as well |
@xackus / @MasterQ32 even a normal zig function can do that: fn somefunc(a: anytype) type {
switch(@typeInfo(@TypeOf(a))) {
.Pointer => .....,
.EnumLiteral => ....,
else => @compileError("invalid type to import"),
}
} |
@MasterQ32 here's a good place for your follow-up proposal: #2206 |
I think that const self = @import("self"); similarly to how you do const builtin = @import("builtin");
const root = @import("root");
const std = @import("std"); Reserving I'd suggest something like |
maybe a builtin similar to |
Isn't a top-level |
@zzyxyzz this proposal is about a way of importing the root of the current package not the current file. This does highlight that importing "self" clashes with an already well used idiom of |
no, it returns the enclosing namespace https://ziglang.org/documentation/master/#This which may or may not be the same thing only if you did it at the top level of the entry point file of the package. this proposal is for anywhere in the package (since they can and usually do span multiple files) |
no one would use the name |
Right. Sorry. Though I wasn't aware that Zig had the concept of a package root. Isn't it anonymous structs all the way down? |
Is this still planned? I would like to use this too because currently the only way to import a package within itself without issues is to use a relative path as mentioned previously and that is undesirable especially when you get into deeper folders. Another option if the self thing has issues could be to allow letting you specify the package you're in by its name, for example if the Package |
+1 Title should probably be updated to "current module" now. |
A few alternate names that could be used are |
Upon encountering the inconvenience, I first thought that What is needed is the ability to import files relative to the package main source directory as seen in the filesystem hierarchy. As I understand from the discussion "self" will just act like "root" and will rely on the I propose:
EDIT: EDIT2: The main point is that we need to be able to import any importable file with a path like syntax relative to the current package directory. |
Uh oh!
There was an error while loading. Please reload this page.
Proposal:
"self"
becomes a special package name (like"root"
currently is), referring to the current package.This allows using
@import("self")
in any file, without specifying a relative path ("../../packagel.zig"
).Originally posted by @andrewrk in #6278 (comment)
The text was updated successfully, but these errors were encountered: