Function hasMethod [src]

Returns true if a type has a name method; false otherwise. Result is always comptime-known.

Prototype

pub inline fn hasMethod(comptime T: type, comptime name: []const u8) bool

Parameters

T: typename: []const u8

Example

test hasMethod { try std.testing.expect(!hasMethod(u32, "foo")); try std.testing.expect(!hasMethod([]u32, "len")); try std.testing.expect(!hasMethod(struct { u32, u64 }, "len")); const S1 = struct { pub fn foo() void {} }; try std.testing.expect(hasMethod(S1, "foo")); try std.testing.expect(hasMethod(*S1, "foo")); try std.testing.expect(!hasMethod(S1, "bar")); try std.testing.expect(!hasMethod(*[1]S1, "foo")); try std.testing.expect(!hasMethod(*[10]S1, "foo")); try std.testing.expect(!hasMethod([]S1, "foo")); const S2 = struct { foo: fn () void, }; try std.testing.expect(!hasMethod(S2, "foo")); const U = union { pub fn foo() void {} }; try std.testing.expect(hasMethod(U, "foo")); try std.testing.expect(hasMethod(*U, "foo")); try std.testing.expect(!hasMethod(U, "bar")); }

Source

pub inline fn hasMethod(comptime T: type, comptime name: []const u8) bool { return switch (@typeInfo(T)) { .pointer => |P| switch (P.size) { .one => hasFn(P.child, name), .many, .slice, .c => false, }, else => hasFn(T, name), }; }