Function hasFn [src]
Returns true if a type has a namespace and the namespace contains name;
false otherwise. Result is always comptime-known.
Prototype
pub inline fn hasFn(comptime T: type, comptime name: []const u8) bool
Parameters
T: type
name: []const u8
Example
test hasFn {
const S1 = struct {
pub fn foo() void {}
};
try std.testing.expect(hasFn(S1, "foo"));
try std.testing.expect(!hasFn(S1, "bar"));
try std.testing.expect(!hasFn(*S1, "foo"));
const S2 = struct {
foo: fn () void,
};
try std.testing.expect(!hasFn(S2, "foo"));
}
Source
pub inline fn hasFn(comptime T: type, comptime name: []const u8) bool {
switch (@typeInfo(T)) {
.@"struct", .@"union", .@"enum", .@"opaque" => {},
else => return false,
}
if (!@hasDecl(T, name))
return false;
return @typeInfo(@TypeOf(@field(T, name))) == .@"fn";
}