How to call an associated function using syn when I have the Type?


#1

Hi all,

I’m trying to create a derive that will call an associated function for a type that satisfies a trait that I have written. I.e. I’m doing something like:

quote!{
  let x = #ty::new();
}

This works as long as the type is not generic, but when the type is generic it expands into something like Option<bool>::new(), where what I want is Option::<bool>::new(). I think what I need is something like a path or a turbofish, but I can’t see how to generate this from a Type, which is all I have.


#2
quote! {
    let x = <#ty>::new();
}

#3

Thanks, that does it!


#4

Additionally, you can use <#ty as Trait>::method() to call “trait associated function”( I’m not sure of the terminology )


#5

Oh, that sounds even better, as I bet it would give better error messages for users of my derive when the type doesn’t implement the trait.