-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
make @import require a string literal rather than a comptime expression #2206
Comments
One thing to consider here is that the path might be a compile time variable from somewhere like |
const tag = @import("build_options").which_package_do_you_want;
const thing = switch (tag) {
.one => @import("one.zig"),
.two => @import("two.zig"),
} This pattern will work for the linked use case, and it will help with static code analysis tools. Is there another use case that would be problematic? |
How would you handle the case of when a user might want to specify a path to include? (e.g. through command line args or maybe a loaded "build configuration" file) |
That could be better handled by having the build file declare a package pointing to that file. |
How would the build file declare a package if Edit: Sorry, realized that this was under the assumption that |
zig build-exe my-source.zig --pkg-begin package_name /some/path/to/package.zig --pkg-end will add a package called |
As @andrewrk suggested, i want to propose the following improvement in addition to "only strings": 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 This change is even non-breaking, as it can be easily fixed with |
Is |
Both proposed |
at the time of writing it appears this change of forcing an Take https://github.com/Snektron/vulkan-zig for example, using this with https://github.com/nektro/zigmod (as opposed to git submodules or something similar) requires the following code: ( const deps = @import("./deps.zig");
const vkgen = @import(deps.pkgs.vkgen.path); |
That could be solved if imports were resolved lazily − I do not know how practical that would be to implement. Implementing this proposal would enable external tools such as ZLS to correctly determine (potential) dependencies. |
is this a separate proposal for this change specifically? |
This doesn't scale very well... At ~20 imports and 5 boards so that grows quickly.. pub const McuFamily = enum {
stm32h7,
//etc...
}
pub const system = @import("bsp/" ++ @tagName(family) ++ "/system.zig"); |
This method was in violation of the accepted proposal ziglang/zig#2206 and disallowed in the compiler as of adc2aed, now in 0.8.0.
In my case, I have a lot of zig files which all expose a function with same name, but the implementation of that function is different. For example: // a.zig
pub fn doSomething() bool {
// do something
}
// b.zig
pub fn doSomething() bool {
// do something differently from a.zig
} Now I want to call the pub fn callByName(name: []const u8) !bool {
const names = [_][]const u8 { "a", "b", "c" }; // this should actually generated automatically by scanning the file system in `build.zig`.
inline for (names) |n| {
const imported = @import("./" ++ n ++ ".zig");
if (@import("std").mem.eql(u8, name, n)) {
return imported.doSomething();
}
}
return error.NotFound;
} If I call However, this can't be done now since |
@hronro how about this? const Import = struct {
name: []const u8,
import: type,
};
pub fn callByName(name: []const u8) !bool {
const imports = [_]Import {
.{ .name = "a", .import = @import("a.zig") },
.{ .name = "b", .import = @import("b.zig") },
};
inline for (imports) |imported| {
if (@import("std").mem.eql(u8, name, imported.name)) {
return imported.import.doSomething();
}
}
return error.NotFound;
} |
@marler8997 but how can you do it without knowing the files? I mean, considering the @hronro comments "// this should actually generated automatically by scanning the file system in I'm quite new to Zig, so I tried to create a custom package in "build.zig":
Then, I tried use @import:
The idea was to create a struct which each fields would have EDIT: The only easy alternative is to create a file in build.zig, such as:
Now, we can import "BackendTypes.zig" that is created by build.zig. However, that sounds wrong for Zig. |
Zig's comptime evaluation is already powerful enough to allows conditional importing without having to have the
@import
target be a comptime expression. For example:This could be rewritten - and I would even say would be preferred to be rewritten - as:
If import syntax is required to be trivially resolvable to either a file or a package name, then Zig can find all the source files for testing without this kind of thing:
zig/test/stage1/behavior.zig
Lines 1 to 19 in 7c38651
There is still a problem though, because if zig ran all tests indiscriminately, this would stop working:
zig/std/os/linux.zig
Lines 1537 to 1541 in 7c38651
The general problem here is that
@import
has compile-time side effects:export
or@export
- potentially conflicting.@compileError
to trigger based on build mode or something else in top levelcomptime
blocks.@cImport
which causes a compile errorIt would be weird to activate these side effects for tests but not for other builds. And these side effects are important; they're not going away.
So I think the benefits of this proposal are not clear. But I think this is a sort of important topic in Zig so it deserves an issue that we can reference.
The text was updated successfully, but these errors were encountered: