-
Notifications
You must be signed in to change notification settings - Fork 50
Open
Description
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;
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels