Making a trait generic over itself?

Hi there, I am trying to make a trait, TensorLike where I have functions that will be generic over two types: one is some numeric type T and the other is some type U: TensorLike<T> such that I can have a function: fn dot(&self, other: &U) -> T; to compute some kind of inner product on my two TensorLike structs.

I was able to solve this by making the function signature use trait objects with: fn dot(&self, other: &dyn TensorLike<T>) -> T; but this seems suboptimal.

Any ideas for how I can get this to work with monomorphisation and generic objects?

I don't really understand what the problem is. Would the following work?

fn dot<T, U>(&self, other: U) -> T
    U: TensorLike<T>;

Thanks so much! That solves it – I hadn't realised I could specify a generic in a method spec inside an impl block. In the end I used: fn dot<U>(&self, other: &U) -> T where U: for<'b> TensorLike<'b, T>

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.