Can I alias Self?

The question is in the title. Reference code:

pub trait A<T, E>: Sized {
    fn foo(value: &mut T) -> Result<(), E>;
    fn bar(value: T) -> Self;

pub trait B: Sized + A<Self::T, Self::E> {
    type T;
    type E;
    fn bar(mut value: Self::T) -> Result<Self, Self::E> {
        // Can I do something like this?
        //  type A = Self as A<Self::T, Self::E>;
        //  match A::foo(&mut value) { ... }
        match <Self as A<Self::T, Self::E>>::foo(&mut value) {
            Ok(_) => Ok(<Self as A<Self::T, Self::E>>::bar(value)),
            Err(error) => Err(error),

It's fine when the code is short. But I have multiple methods inside trait B, all using functions of trait A. It gets messy very fast.

Maybe you just need to present a better example where this won’t work, but in this case just writing things like Self::foo and A::bar and letting the compiler infer the rest seems to solve your problem?

Creating a shorthand for Self as A<Self::T, Self::E> is not a thing that can work, really, because it’s not a thing by its own, but part of the fully qualified call syntax <TYPE as TRAIT>::ITEM. You can’t have the TYPE as TRAIT part of this syntax by itself.

No, it actually works, I just forgot that I can do this. Thank you!

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.