-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed as not planned
Labels
proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone
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();
}
cryptocode, EspeuteClement, T1nk3r1, silversquirl, sno2 and 26 moreMatthiasPortzelsilversquirl, SuperAuguste, tensorush, michaelbartnett, notsatvrn and 1 moremaxmilton
Metadata
Metadata
Assignees
Labels
proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.This issue suggests modifications. If it also has the "accepted" label then it is planned.