| ... | ... | @@ -0,0 +1,25 @@ |
| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | |
| 5 | pub 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 | |
| 17 | test { |
| 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 | } |