Passing Generic Type as Type Parameters
Hello everyone,
I hope this is not a stupid question but I was wondering about the following.
I'd like to achieve the following:
struct MyStruct<A, B, C, M> { map: M<A, C<B>>; }
Such that I can do something like: MyStruct<i32, i32, Vec, HashMap>. The idea is that the user of the struct is able to pick the map and collection that is most efficient for his uses.
For as far as I know we cannot pass generics as Type Arguments. Is there any way around this without too much overhead?
Posted Oct 15, 2018, 6:00 AM UTC
The general feature you're looking for is called higher kinds, and Rust is hopefully getting a version of them soon (in the form of generic associated types, or GATs for short). Today, however, you can't write this code. :(
(Do note that GATs do not amount to quantification over type constructors, so it does not introduce higher kinded type polymorphism; however, you get much of the same power - but not ergonomics...)
Thanks for your replies! I will look into this :) Do you believe GATs still needs a lot of time before it enters the language?
Is this intended to replace PhatomData?
No,
PhantomDatais completely unrelated to this.