#how to do a comptime access of a `pub` function declaration by its name (`[]const u8`)

1 messages · Page 1 of 1 (latest)

terse spear
#

I want to access a function ptr using its stringified name at comptime.
Is this possible?

const Self = @This();
pub fn foo() u32 { return 123 ; }

const foo_func = get_decl(Self, "foo");
// std.debug.assert(foo_func() == 123);
earnest flume
terse spear
#

Apparently, hasDecl also works. But access has to be done using field.

earnest flume
#

Works on both fields and declarations.
it's essentially equivalent to the dot syntax (super.sub), no matter in what form (that is, equivalent to the DOT IDENTIFIER choice of the SuffixOp nonterminal defined in the grammar)

terse spear
#

got it

earnest flume
#

@hasDecl will only return a boolean, not the item itself

terse spear
#

I don't always want to parse.