How to reliably check if a type has a function?

The module std.os.linux contains a declaration of pub const syscall7 = syscall_bits.syscall7;, so @hasDecl(std.os.linux, "syscall7") is true.

On my platform, syscall_bits.syscall7 actually evaluates to @import("linux/x86_64.zig").syscall7, and this module does not contain a syscall7, so @field(std.os.linux, "syscall7") leads to a compile error.

As a result, I cannot compile a program to iterate the declarations of a type. I have to manually exclude all functions like syscall7 from the loop. Is there any reliable method to query the existence of a function?

const std = @import("std");

pub fn main() !void {
    std.debug.print("syscall0:\n", .{});
    std.debug.print("\thas decl: {}\n", .{@hasDecl(std.os.linux, "syscall0")});
    std.debug.print("\thas field: {}\n", .{@hasField(std.os.linux, "syscall0")});
    std.debug.print("\thas fn: {}\n", .{std.meta.hasFn(std.os.linux, "syscall0")});

    std.debug.print("syscall7:\n", .{});
    std.debug.print("\thas decl: {}\n", .{@hasDecl(std.os.linux, "syscall7")});
    std.debug.print("\thas field: {}\n", .{@hasField(std.os.linux, "syscall7")});
    // Comment the following line to get a compile error
    // std.debug.print("{}\n", .{std.meta.hasFn(std.os.linux, "syscall7")});
}

You should be able to use field, but point to the local name of that @import() Something like this:

const Constants = @import("root.zig").Constants;
pub fn main(init: std.process.Init.Minimal) !void {
    std.log.info("Field: {}", .{@field(@This(), "Constants")});
...

And I was able to see it:

j.markovic@MacBook-Pro-2 ~/GIT/sortcp/src (git)-[union] % zig build run -- config.zig
2026-03-21 10:57:44.938 info: Field: root.Constants

Thanks for your reply, but I don’t quite get it. How can I turn your solution into a function to return false when the input type is std.io.linux and the query name is syscall7? Should I create a local name for every declaration of the type info?

From my testing, you can use @hasDecl() to check if the declaration is there. Now, @field() currently on latest dev is panicking with panic: TODO if it is used like this:
std.log.info("field: {} ", .{@field(@This(), "createFn")});

@field() is accessing it, so you need to pass it where the call will be made and this works in my code:

switch (cfg.sort) {
    inline else => |tag| {
        // The compiler resolves 'tag' at comptime for each branch it generates
        std.mem.sortUnstable(
            []const u8,
            buffers.file.items,
            @as(Order, .lt),
            // Using @field to get the function corresponding to the tag name
            @field(sortcp, @tagName(tag) ++ "Sort"),
        );
    },
}

All in all, from testing, @hasField() does not work for functions, and you need to use @hasDecl() which works:

const createFn = @import("timestamp.zig").Time.create;
pub fn main(init: std.process.Init.Minimal) !void {
    _ = init;
    std.log.info("hasField: {} ", .{@hasField(@This(), "createFn")});
    std.log.info("hasDecl: {} ", .{@hasDecl(@This(), "createFn")});
    // std.log.info("field: {} ", .{@field(@This(), "createFn")});
    std.log.info("hasField: {} ", .{@hasField(@This(), "NotExist")});
    std.log.info("hasDecl: {} ", .{@hasDecl(@This(), "NotExist")});
}

Output:

j.markovic@MacBook-Pro-2 ~/GIT/zig-playgorund/src % zig run logging.zig                   
Mar 21 12:28:37 info: hasField: false 
Mar 21 12:28:37 info: hasDecl: true 
Mar 21 12:28:37 info: hasField: false 
Mar 21 12:28:37 info: hasDecl: false 

I don’t think there’s a good compile-time way to do it, though I’ll be happy to stand corrected. You could probably figure something out at build time by inspecting source.

Probably the simplest solution is to edit the source. I know that there were efforts the clean up some of this behavior recently, replacing ‘untouchable’ decls with ‘placeholder’ decls - in this case, a simple change to the source like

pub const syscall7 = (if @hasDecl(syscall_bits, “syscall7”)) syscall_bits.syscall7 else void;

would have saved you, provided you know how to handle void.

Indeed, I can figure it out by reading source code, but my original intention was to dump all signatures of an unfamiliar module so that I don’t have to read the code. :slightly_frowning_face:

You could still do something generic in your build script - make a blacklist of untouchable decls, populate it by attempting to do your decl walk in a process, capturing its output and parsing the compile errors…

But if you go the route of editing the source to make none the decls be untouchable, that might be useful to others and you could maybe get a PR out of it?