fn tagName(comptime E: type, e: E) ?[]const u8

A safe alternative to @tagName() for non-exhaustive enums that doesn’t panic when e has no tagged value. Returns the tag name for e or null if no tag exists.

Parameters

E: type,
e: E,

DocTests

test tagName {
    const E = enum(u8) { a, b, _ };
    try testing.expect(tagName(E, .a) != null);
    try testing.expectEqualStrings("a", tagName(E, .a).?);
    try testing.expect(tagName(E, @as(E, @enumFromInt(42))) == null);
}