@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" {
try 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.
try expect(@hasDecl(Foo, "hi"));
// @hasDecl is for declarations; not fields.
try expect(!@hasDecl(Foo, "nope"));
try expect(!@hasDecl(Foo, "nope1234"));
}
``` ```
$ zig test test.zig
Test [1/1] test "@hasDecl"...
All 1 tests passed.
See also:
- @hasField
