zig-ecs
zig-ecs copied to clipboard
@typeName is not guaranteed to be unique, and in fact has no guarantees
Never rely on the output of @typeName: its return value will more than likely end up being implementation-defined (if @typeName isn't removed from the language altogether, that is).
This could be fixed by making component types known at comptime, or with the alternative suggested by mlugg in the same comment:
const std = @import("std");
const TypeId = *const struct {
_: u8,
};
pub inline fn typeId(comptime T: type) TypeId {
return &struct {
comptime {
_ = T;
}
var id: @typeInfo(TypeId).pointer.child = undefined;
}.id;
}
pub fn main() !void {
@compileLog(typeId(u8) == typeId(u8)); // comptime-known
@compileLog(typeId(u8) == typeId(u16)); // and works correctly
@compileLog(typeId(u16) == typeId(u16));
passAtRuntime(typeId(u8), typeId(u16));
}
fn passAtRuntime(a: TypeId, b: TypeId) void { // and you can pass them at runtime too
_ = a;
_ = b;
}
The good old struct reference trick. I sure do wish zig had a typeId set at comptime for all types but this is the way we have to do things to get one in userland.