-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
introduce @hasField builtin function #1439
Comments
Which folders should be searched? In what order? If found in more than one place, shouldn't that information be returned? |
Sorry if this is hijacking the issue thread, but on the topic of feature detection, are we talking about runtime or comptime variables? Like a feature of a filesystem or a feature of the standard library? Feature detection may be possible with status quo zig. |
the current scope
probably both, just if the symbol is defined, so even with a runtime variable it would be a comptime if it exists.
Yes it is, but there are times where the feature could be expressed better in a symbol existing. Here is a use-case: would be better expressed as |
it is a compile error to define the same symbol twice |
The target may be a different machine. Also - random idea - could environment variables be symbols? This would be relevant to the build system. |
environment variables are horribly wasteful of memory and are generally a bad idea. |
@shawnl this issue as it is stated in the title and original post is a proposal for a language feature. We need a lot more information for this to have a chance at being accepted. How does it work? What does the status quo workaround look like? How is adding a language feature justified in this situation? Remember that zig wants to stay simple in general unless the complexity is worth it. Feature detection can be a general discussion, not necessarily related to the proposed language feature. It seems like discussing how feature detection should work is an easier topic to start with. Is that what this is all about? If so, let's focus on that. Thanks for the link to the commit, but i didn't quite understand the point. That code is years old with workarounds for bugs that were fixed years ago. Can you explain that use case in more detail? |
I believe the example use case would be solved if #1047 is implemented. Then such a feature could easily be implemented in userland by iterating over the typeInfo of the 'namespace'. |
@thejoshwolfe Issue updated. I think it is now clear. @tgschultz Indeed that would fix this use-case and in a more general manner. |
Ah thanks. I see now that these symbols are being defined by a pub use switch (builtin.arch) {
builtin.Arch.x86_64 => @import("x86_64.zig"),
builtin.Arch.i386 => @import("i386.zig"),
else => @compileError("unsupported arch"),
}; I've always hated Here's a different workaround for this specific issue: const arch_stuff = switch (builtin.arch) {
builtin.Arch.x86_64 => @import("x86_64.zig"),
builtin.Arch.i386 => @import("i386.zig"),
else => @compileError("unsupported arch"),
};
pub use arch_stuff;
...
if (isDefined(arch_stuff, "SYS_mmap2")) {
...
} And now |
so as i understand it this is blocked on #1047 So for that reason we still need a way to search a struct by identifier. it appears they are stored in a hash map |
Yeah, i think some kind of get-by-name api in TypeInfo makes sense. |
On IRC andrew said this could be
|
You could get the type with |
my bad. I actually meant it returns the field. I just don't know what the type of the field is so I put |
How about Also, at this point, why have |
Hmm. I'm going to stick with my original idea for now. Let's run with this and see what the code ends up looking like, and then we can re-evaluate. So to make it clear, what is accepted is |
This was quite straight-forward Closes: ziglang#1439
Hmm, I realized this doesn't cover it. There also needs to be a way to examine whether something is defined in a container---like a constant or a function. This has to be a differn't builtin because it is a totally differn't thing. |
@shawnl const S = struct {
a: u8,
const A = 0;
};
test "" {
var s: S = undefined;
@import("std").testing.expect(@hasField(s, "a"));
@import("std").testing.expect(@hasField(S, "A"));
} |
I think the terminology needs some consideration. "Field" is overloaded here and refers to both variables within a namespace and struct instance field.
which is less obvious, I think, than if there were a separate builtin:
Consider also the following: in order to determine if an instance of a struct would have a certain member, you must initialize an instance: However, one can imagine there might be usecases where one would want to substitute an empty struct in place of a struct instance to a comptime function and have the same code work. Personally, I think it would be better to have separate builtins for each case, but failing that I propose the following nomenclature to avoid confusion: "member" => either a struct instance variable or a namespaced variable. which would mean that |
@tgschultz yes, that is basically the conclusion I came to (that the current If @andrewrk accepts your terminology than my patch is still good to go, and I can work on the rest. |
I needed this (at least |
|
I agree that |
I agree with this. |
Accepted Proposal
Using a undefined symbol is a compiler error:
It is possible to work around this by using buildin.arch and listing which arches have mmap2 (the 32-bit ones)
But instead of making all these assumption with code like that it would be cleaner to be able to comptime branch of a identifier existing:
The text was updated successfully, but these errors were encountered: