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

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:

  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.

1 Like
quote! {
    let x = <#ty>::new();

Thanks, that does it!

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

1 Like

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.

1 Like