#function reflection

1 messages · Page 1 of 1 (latest)

maiden igloo
#

Can someone point me to example code that demonstrates using reflection to get information about the parameters and return type of a function?

dark mantle
maiden igloo
#

Thanks! I was able to use that to determine how to get the return type of a function. But that only gets information about the first parameter. I haven't been able to get the syntax right to iterate over all the parameters with a for loop. I get the error "error: values of type '[]const builtin.Type.Fn.Param' must be comptime-known, but index value is runtime-known". I probably need to add the comptime keyword somewhere, but haven't figured that out. Here is my code:

    const T = @TypeOf(next); // next is a function
    print("next type is {}\n", .{T});
    const info = @typeInfo(T);
    const return_type = info.Fn.return_type;
    print("return type is {any}\n", .{return_type});
    for (info.Fn.params) |param| {
        print("param = {any}\n", .{param.type});
    }
dusky quiver
#

try doing an inline for

maiden igloo
#

That was it. Thanks!

#

I see there is no std.meta.trait.isFunction. What's the best way to test if a type is a function? Should I check for the existence of @typeInfo(T).Fn?

#

Basically I'm looking for a way to test if a type is a function type.