2

Is it possible to store different Rust types into a Vec in some form that would allow me to get the types at later date?

I could store the TypeId of any types, but I couldn't then use the TypeId to get back to the original types, as far as I know.

In Java terms, I want to create a [boolean.class, char.class, ...], etc.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Daniel Fath
  • 16,453
  • 7
  • 47
  • 82
  • 7
    What do you want to do with the types later? – Chris Emerson Oct 21 '16 at 15:32
  • 3
    ^ and... have you looked into traits? – Matthieu M. Oct 21 '16 at 15:40
  • @ChrisEmerson: I have a heterogeneous list, that I want to type check. I used some type shenanigans to have all it's values in `Vec>`, but extracting the types gets a bit... hard and I assume slow since the type is essentially a `Cons>`. – Daniel Fath Oct 21 '16 at 15:48
  • 1
    I'm not sure what you mean by typechecking a heterogenous list, and extracting types. Can you show some pseudo code for what you want to do? – Chris Emerson Oct 21 '16 at 15:50
  • @MatthieuM. Yes, although not sure what you **exactly** mean? Storing different traits in Vector instead of types? – Daniel Fath Oct 21 '16 at 15:51
  • Agree with @ChrisEmerson, but perhaps **not** by changing *this* question, as it already has two answers for the current phrasing. – Shepmaster Oct 21 '16 at 15:51
  • 1
    @DanielFath: You need something homogeneous in a `Vec`, but types could implement a single `trait` that returns what kind of type is stored in it (if the list of kind if finite) or you could use an `enum` if the list of types/kinds is well-known in advance, etc... You may want to ask a new question presenting the code you have written with `Cons` and asking how to do it better: with an exact example of your problem, the answers will be more relevant. – Matthieu M. Oct 21 '16 at 15:55
  • Yeah, it's a bit hard to explain. But imagine you have `let x = Cons<32, Cons>>` and you want to extract the `"string"` from it. I was hoping to extract the type from a paired vector of types, rather than search recursively. – Daniel Fath Oct 21 '16 at 15:57

2 Answers2

6

No. Types are not a runtime construct and are removed during compilation. You cannot have a collection of the types themselves because they don't exist.

Most of the time, you want to create a heterogeneous collection of objects that all adhere to some shared interface (a.k.a. a trait).

Community
  • 1
  • 1
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
4

The only reason that Integer.class is useful in Java, is because Java implements reflection:

  • the runtime has a description of all existing types
  • for each type, it has a description of all attributes and methods

and therefore the virtual machine can, at runtime, create an instance of a type from thin air.


Automatic, pervasive reflection violates at least one core tenet of Rust:

You don't pay for what you don't use.

so has not been implemented.

Rust has some amount of reflection still:

  • it has compile-time reflection, via plugins
  • it has some type information, via TypeId

However, the former does not incur any memory/performance overhead and the latter is on a pay-per-use basis.

As far as I know, there is no proposal to significantly extend reflection yet. Even down-casting is for now explored as a library, and not a language, option (see the query_interface crate).

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722