Skip to content

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

Closed as duplicate

Description

@prime31

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.

Activity

  1. added
    proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.
    on May 28, 2020
  2. added this to the 0.7.0 milestone on May 28, 2020
  3. modified the milestones: 0.7.0, 0.8.0 on Oct 27, 2020
  4. modified the milestones: 0.8.0, 0.9.0 on May 19, 2021
  5. mmstick commented on Jun 3, 2021

    @mmstick

    This would be instrumental to creating an ECS library in Zig

  6. ikskuh commented on Jun 3, 2021

    @ikskuh
    Contributor

    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

  7. mmstick commented on Jun 3, 2021

    @mmstick

    Awesome. Now I can at least complete my ECS

  8. modified the milestones: 0.9.0, 0.10.0 on Nov 23, 2021
  9. modified the milestones: 0.10.0, 0.11.0 on Apr 16, 2022
  10. modified the milestones: 0.11.0, 0.12.0 on Apr 9, 2023
  11. modified the milestones: 0.13.0, 0.12.0 on Jul 9, 2023
  12. Parallel-Paradox commented on Aug 22, 2023

    @Parallel-Paradox

    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

  13. modified the milestones: 0.14.0, 0.15.0 on Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions