Issue with ty in macro rules

Consider an example where a type is to be plugged in as a part of an expression:

macro_rules! new {($t: ident) => {$t::new()};}

fn main() {
    let _: Vec<i32> = new!{Vec};
}

Actually we would like to use ty instead of ident or tt, but that is not possible. It has to be expressed this way:

macro_rules! new {($t: ty) => {<$t>::new()};}

fn main() {
    let _: Vec<i32> = new!{Vec<i32>};
}

But now it is not allowed to omit the type arguments altogether, i.e. it is not allowed to write Vec instead of Vec<i32> or Vec<_>.

Is there any specific reason why $t::new() should not be permitted for $t: ty or could we fix that?

Maybe you need path in this case?

I guess that if it’s a type, it has to be unambiguously representable as such in the ast, so an incomplete one isn’t?

https://doc.rust-lang.org/1.0.0/syntax/ast/enum.Ty_.html

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