Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] @typeInfo of a type with a unique identifier #5459

Open
prime31 opened this issue May 28, 2020 · 4 comments
Open

[Feature Request] @typeInfo of a type with a unique identifier #5459

prime31 opened this issue May 28, 2020 · 4 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@prime31
Copy link
Contributor

prime31 commented May 28, 2020

Currently, there is no way to key a HashMap with a type to make a lookup table. It would be handy if we could get at a unique value to use for this use case: @typeInfo(T).id. If @typeInfo(T).id were unique to each type it would provide the perfect key for a type lookup table.

The current workaround is to use a StringHashMap keyed off of @typeName(T) with the values being auto-incremented ints.

@Vexu Vexu added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label May 28, 2020
@Vexu Vexu added this to the 0.7.0 milestone May 28, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 27, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@mmstick
Copy link

mmstick commented Jun 3, 2021

This would be instrumental to creating an ECS library in Zig

@ikskuh
Copy link
Contributor

ikskuh commented Jun 3, 2021

There's a workaround that takes up 1 byte of memory per type:

fn typeId(comptime T: type) usize {
    return @ptrToInt(&struct { var x: u8 = 0; }.x);
}

But i'd prefer to have proper @typeId builtin or a field in @typeInfo that solves this use case

@mmstick
Copy link

mmstick commented Jun 3, 2021

Awesome. Now I can at least complete my ECS

@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
@Parallel-Paradox
Copy link

There's a workaround that takes up 1 byte of memory per type:

fn typeId(comptime T: type) usize {
    return @ptrToInt(&struct { var x: u8 = 0; }.x);
}

But i'd prefer to have proper @typeId builtin or a field in @typeInfo that solves this use case

Change @ptrToInt into @intFromPtr after 0.11.0

@andrewrk andrewrk modified the milestones: 0.14.0, 0.15.0 Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

6 participants