| author | |
| committer | |
| log | b8fec06fa26ec5ddd2b1b9cb5099de3a9df9109f |
| tree | 9d72f0ed7b48f385c924b2e9668856619655c38e |
| parent | d9ffe3263a8d50114376b3eebaf6ab634332e2e2 |
2 files changed, 26 insertions(+), 0 deletions(-)
src/hasFn.zig created+25| ... | @@ -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 | } | ||
src/lib.zig+1| ... | @@ -103,3 +103,4 @@ pub usingnamespace @import("./indexOfSlice.zig"); | ... | @@ -103,3 +103,4 @@ pub usingnamespace @import("./indexOfSlice.zig"); |
| 103 | pub usingnamespace @import("./mapBy.zig"); | 103 | pub usingnamespace @import("./mapBy.zig"); |
| 104 | pub usingnamespace @import("./lessThanBy.zig"); | 104 | pub usingnamespace @import("./lessThanBy.zig"); |
| 105 | pub usingnamespace @import("./isContainer.zig"); | 105 | pub usingnamespace @import("./isContainer.zig"); |
| 106 | pub usingnamespace @import("./hasFn.zig"); |