# Type\_id\_as\_type!

**URL:** <https://internals.rust-lang.org/t/type-id-as-type/22293>\
**Category:** language design\
**Created:** [January 29, 2025, 10:58pm UTC](https://internals.rust-lang.org/t/type-id-as-type/22293 "2025-01-29T22:58:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [January 29, 2025, 10:58pm UTC](https://internals.rust-lang.org/t/type-id-as-type/22293/1 "2025-01-29T22:58:34Z")

</div>

not familiar with compiler internals, so i'm not sure how feasible this is, but i realized that with a single magic macro we could make rust generics basically as powerful as zig comptime.

the argument would be a const expression that evaluates to a `TypeId`, and it would expand to a magic type expression corresponding to the type of a `TypeId`.

this would be a huge extension to the rust language, but it would be interesting as a compiler experiment.

---

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [January 29, 2025, 11:39pm UTC](https://internals.rust-lang.org/t/type-id-as-type/22293/2 "2025-01-29T23:39:56Z")

</div>

it's also worth noting this would make it possible to implement `typeof` in a limited capacity.

---

<div class="post-metadata">

**Author:** ![DanielFath](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielfath/32/5372_2.png) [@DanielFath](https://internals.rust-lang.org/u/DanielFath)\
**Post date:** [January 30, 2025, 9:46am UTC](https://internals.rust-lang.org/t/type-id-as-type/22293/3 "2025-01-30T09:46:22Z")

</div>

Isn't this just compile time reflection in Rust?

Not sure how you imagine it would work though? In `fn type_id_as_type!(type_id: TypeId) -> ???` what are inputs and outputs (I assumed it's `TypeId`)?

I did some pondering on this, but my interests pulled me towards YAML and SIMD (an unholy combination, if there ever was one).

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [January 30, 2025, 5:58pm UTC](https://internals.rust-lang.org/t/type-id-as-type/22293/4 "2025-01-30T17:58:32Z")

</div>

`TypeId` is currently a one directional 128-bit hash of type information. However, the compiler does maintain a map of generated `TypeId`s to ensure no accidental collisions happen (within a statically linked unit), so it would be theoretically possible to go from `TypeId` back to type. I don't see how it helps the expressive power of generics, though. Namely, a significant part of the expressive power of Zig's comptime is that they're syntactic templates that do name resolution after substitution of the comptime arguments, whereas Rust generics, even if we extend const generics to allow `TypeId`, are checked before instantiation and must be well formed for any and all instantiation that satisfies the trait bounds.

---

<div class="post-metadata">

**Author:** ![Noratrieb](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/noratrieb/32/12440_2.png) [@Noratrieb](https://internals.rust-lang.org/u/Noratrieb)\
**Post date:** [January 30, 2025, 7:05pm UTC](https://internals.rust-lang.org/t/type-id-as-type/22293/5 "2025-01-30T19:05:03Z")

</div>

The compiler does not store any tables of type ids today, collisions are prevented by using a 128-bit hash. Adding such a table wouldn't necessarily be impossible, but it would quite tricky to do with the compilers query structure I think.

---

<div class="post-metadata">

**Author:** ![DanielFath](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielfath/32/5372_2.png) [@DanielFath](https://internals.rust-lang.org/u/DanielFath)\
**Post date:** [January 31, 2025, 9:46am UTC](https://internals.rust-lang.org/t/type-id-as-type/22293/6 "2025-01-31T09:46:03Z")

</div>

When I was looking into compile time macros. What I did was create a derive macro that for a given `T: Reflect` will invoke a (`const`) method on that type, and that `TypeInfo` will contain all the info needed to construct a full descriptor of value. But to achieve that, I ran into problems:

1. You can't define a (although you can resort to type hackery and make a `impl Foo { const fn typeinfo() -> TypeInfo {...} }`)

```rust
trait Reflect {
  const fn reflect() -> TypeInfo
}

```

making it non-composable.

1. It's not possible to construct an enum from [`Discriminant`](https://doc.rust-lang.org/std/mem/fn.discriminant.html), even if it's possible to extract.
2. If I recall correctly, there were some issues with functions being essentially variable argument generics, i.e. heterogeneous lists, while having no way to express that in code.

---

<div class="post-metadata">

**Author:** ![the8472](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@the8472](https://internals.rust-lang.org/u/the8472)\
**Post date:** [January 31, 2025, 1:26pm UTC](https://internals.rust-lang.org/t/type-id-as-type/22293/7 "2025-01-31T13:26:46Z")

</div>

> [@CAD97](#):
>
> I don't see how it helps the expressive power of generics, though. Namely, a significant part of the expressive power of Zig's comptime is that they're syntactic templates that do name resolution after substitution of the comptime arguments, whereas Rust generics, even if we extend const generics to allow `TypeId`, are checked before instantiation and must be well formed for any and all instantiation that satisfies the trait bounds.

I think this could be done for associated types and consts, basically turning a TypeId const into the output type. Though it'd require a mono-time error if the computed type doesn't fulfill the bounds.

---

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [January 31, 2025, 11:35pm UTC](https://internals.rust-lang.org/t/type-id-as-type/22293/8 "2025-01-31T23:35:05Z")

</div>

the idea is it would let you conditionally include fields and such.

for example, a datastructure may be able to become a ZST when instantiated with ZST type arguments.

i guess technically this overlaps with specialization?

---

<div class="post-metadata">

**Author:** ![jdahlstrom](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jdahlstrom/32/3351_2.png) [@jdahlstrom](https://internals.rust-lang.org/u/jdahlstrom)\
**Post date:** [February 1, 2025, 9:38am UTC](https://internals.rust-lang.org/t/type-id-as-type/22293/9 "2025-02-01T09:38:23Z")

</div>

I still don't understand how you envision the macro would be used. Could you provide some concrete example code?

---

<div class="post-metadata">

**Author:** ![DanielFath](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielfath/32/5372_2.png) [@DanielFath](https://internals.rust-lang.org/u/DanielFath)\
**Post date:** [February 1, 2025, 9:38am UTC](https://internals.rust-lang.org/t/type-id-as-type/22293/10 "2025-02-01T09:38:29Z")

</div>

> [@binarycat](#):
>
> for example, a datastructure may be able to become a ZST when instantiated with ZST type arguments.

Could you give me an example of what you mean? Types can be conditionally ZST.

---

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [February 4, 2025, 9:49pm UTC](https://internals.rust-lang.org/t/type-id-as-type/22293/11 "2025-02-04T21:49:09Z")

</div>

for example, a Vec type with an opaque capacity could remove the capacity and heap pointer fields altogether when instantiated with a ZST, and just track the length field.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [July 29, 2026, 9:49pm UTC](https://internals.rust-lang.org/t/type-id-as-type/22293/12 "2026-07-29T21:49:51Z")

</div>

Closed on July 29, 2026, 9:49pm UTC

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
