I've seen here in the discord a couple times this typeId trick but didn't have reason to use it till now. MasterQ32 also made a post about this: https://zig.news/xq/cool-zig-patterns-type-identifier-3mfd
pub fn typeId(comptime T: type) usize {
_ = T;
const H = struct {
var byte: u8 = 0;
};
return @ptrToInt(&H.byte);
}```
Thing is, when used in a `comptime` context the last line becomes a compile error:
error: unable to evaluate comptime expression
return @intToEnum(TypeId, @ptrToInt(&H.byte));
^~~~~~~~~~~~~~~~~~~~~~~
note: operation is runtime due to this operand
return @intToEnum(TypeId, @ptrToInt(&H.byte));
^~~~~~~~~~~~```
I found the same message here: https://github.com/ziglang/zig/issues/2352#issuecomment-1279902162
Is this currently bugged or am I doing something wrong?
The language reference states this:
Zig is able to preserve memory addresses in comptime code, as long as the pointer is never dereferenced.