Closed as not planned
Description
Add a new builtin called @typeId
:
@typeId(comptime T: type) usize
This builtin returns a unique integer for each type passed, and will return the same integer for the same type.
The return value must not be consistent inbetween builds, so a second build might return completly different numbers
for the same types.
An alternative variant might return u32
or u64
to have a stable interface between different platforms.
Use cases
- Type erasure
- Additional programmatic type safety
- Variadic in-memory serialization
Prior art:
User-land implementation
The following version is runtime only, as we can't perform intFromPtr at compiletime:
fn typeId(comptime T: type) TypeId {
const Tag = struct {
var name: u8 = @typeName(T)[0]; // must depend on the type somehow!
inline fn id() TypeId {
return @enumFromInt(@intFromPtr(&name));
}
};
return Tag.id();
}