-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
std.builtin.TypeInfo.Declaration.data.Fn.arg_names is empty #8259
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
The argument types are available and so are the argument names. |
Sorry for the stupid question, but I'm new to zig (2 days of experience with the language), and from tutorials, I got only the reference to |
Related: #6530 |
Probably it's indeed related somehow, but maybe you can answer my question directly? I browsed through the "related" thing, don't have a clue how it can help me. I'm doing a very important experiment in the history of dart language, no one has ever tried to integrate it with zig and 99.9% of dart users never heard of it, to begin with, and even if they heard, they heard it from me!. In case of success, my plan was to start spamming dart mailing list extolling the virtues of a new language and alleging that it fits perfectly into ffi framework etc.. But I can't get ahold of a damn Declaration, without which not only can't I accomplish my plan - I can't even start putting it in motion! 😢 |
You can't obtain the argument names directly from function type info. Argument names are not part of function types. Instead, you need to pull arguments from a specific function declaration site. To do this, you must find the Declaration entry in the declaring type. There is no other way. So for example: const Foo = struct {
pub fn myFunc(a: u32) void {}
};
comptime {
// get info for declaring type
const info = @typeInfo(Foo);
// get the myFunc decl. There's only one.
const decl = info.Struct.decls[0];
// get the argument names
const args = decl.data.Fn.arg_names;
// args is [_][]const u8{ "a" }
} |
So in order to get the names of function arguments, I have to declare this function inside some Struct? And it won't work for a standalone function? E.g. I have pub fn myFunction(m: usize, N: i32) u32 {
//...
} Do I have to put it into a struct? const t=@TypeOf(Foo.myFunc);
const typeInfo = @typeInfo(t);
const myArgs = typeInfo.Fn.args;
// and from there, I get all types Right? |
The top-level of any file is itself a struct. You can use The argument types are stored in the type info for the function type. |
|
Yes, |
Everything works, except arg_names. The list of arg_names is always empty const Foo = struct {
pub fn myFunc(a: u32, b:u32, c:u32) void {}
}; | *"fn_type=", fn(u32, u32, u32) void |
I'm able to reproduce this as well. This code prints const Foo = struct {
pub fn myFunc(a: u32) void {}
};
comptime {
// get info for declaring type
const info = @typeInfo(Foo);
// get the myFunc decl. There's only one.
const decl = info.Struct.decls[0];
// get the argument names
const args = decl.data.Fn.arg_names;
// args is [_][]const u8{ "a" }
@compileError(std.fmt.comptimePrint("args count: {d}", .{args.len}));
} |
|
I'm trying to integrate zig with dart. Dart supports ffi with C. For the purposes of my project, zig would be a better choice than C. I need only 1-way calling : dart -> zig.
To make this possible, I have to generate a bit of glue code for dart. It would be enough for me to be able to get
@typeInfo
for my zig functions that includes the argument names and argument types. Currently,@typeInfo
does not seem to provide this information.Is there any workaround?
The text was updated successfully, but these errors were encountered: