1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4
5pub fn hasFn(comptime name: []const u8) fn (type) bool {
6 const Closure = struct {
7 pub fn trait(comptime T: type) bool {
8 if (!comptime extras.isContainer(T)) return false;
9 if (!comptime @hasDecl(T, name)) return false;
10 const DeclType = @TypeOf(@field(T, name));
11 return @typeInfo(DeclType) == .@"fn";
12 }
13 };
14 return Closure.trait;
15}
16
17test {
18 const TestStruct = struct {
19 pub fn useless() void {}
20 };
21
22 try std.testing.expect(hasFn("useless")(TestStruct));
23 try std.testing.expect(!hasFn("append")(TestStruct));
24 try std.testing.expect(!hasFn("useless")(u8));
25}