Can I call trait associated type method directly inside `impl trait` function declaration?

My code is like below, I tried to write fn test_one but the compiler wasn't happy about it. I can easily re-write it to fn test_two but I am wondering if I can make test_one works. Or I have to write like fn test_two. Thanks!

trait Inside {
    fn inside_method(i: i32) -> i32;

trait Wrapper {
    type Ins: Inside;

struct W;

impl W {
    // fn test_one(i: i32, wp: impl Wrapper) -> i32 {
    //     <wp as Wrapper>::Ins::inside_method(i)
    // }

    fn test_two<W: Wrapper>(i: i32, wp: W) -> i32 {

<Type as Trait> syntax is for types only, not for values. I don't think you can name type of the impl Wrapper, so this syntax won't work.

You can use the value to call another function that names the type, so Self::test_two(i, wp) works :slight_smile:

1 Like

Well, you could use a helper function:

fn test_one(i: i32, wp: impl Wrapper) -> i32 {
    fn helper<W: Wrapper>(_: &W, arg: i32) -> i32 {
    helper(&wp, i)

Besides this, I don't think it's possible.

1 Like

Note the parallel on how if you need to name a lifetime for some reason, you replace the elided form in the argument list with an explicit name in the generic list.

fn one(_: &str, _: impl Wraper) {}
// -->
fn two<'s, T: Wrapper>(_: &'s str, _: T) {}
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.