@hasDecl

@hasDecl

@hasDecl(comptime Container: type, comptime name: []const u8) bool

Returns whether or not a struct, enum, or union has a declaration matching name.

test.zig

const std = @import("std");
const expect = std.testing.expect;

const Foo = struct {
    nope: i32,

    pub var blah = "xxx";
    const hi = 1;
};

test "@hasDecl" {
    expect(@hasDecl(Foo, "blah"));

    // Even though `hi` is private, @hasDecl returns true because this test is
    // in the same file scope as Foo. It would return false if Foo was declared
    // in a different file.
    expect(@hasDecl(Foo, "hi"));

    // @hasDecl is for declarations; not fields.
    expect(!@hasDecl(Foo, "nope"));
    expect(!@hasDecl(Foo, "nope1234"));
}
``` ```
$ zig test test.zig
1/1 test "@hasDecl"... OK
All 1 tests passed.

See also:

  • @hasField