mod sealed {
// As expected, making this private will cause T::SealAssoc to not work, even in impl Ans
pub trait Sealed { type SealAssoc; }
pub trait Res: Sealed {}
}
use sealed::Res;
pub trait Ans {
//type Dep; fn bleh(dep: Self::Dep);
type Dep: Res; fn bleh(dep: Self::Dep::SealAssoc);
}
impl<T: Res> Ans for T {
//type Dep = T::SealAssoc; fn bleh(_: Self::Dep) {}
type Dep = T; fn bleh(_: Self::Dep::SealAssoc) {}
}
I see that now, one should rather make the sealed traits private to avoid exposing publicly (even if hidden) the sealed trait's associated consts and types (as per Trait with private items - #8 by tczajka).
I find that it is true that I can use the associated type from the sealed trait, but only in some conditions:
- If I try to compile the code as it is in the playground, it fails to compile, saying it's impossible to access the associated type.
- If I swap the comment on the
pub trait Ansandimpl Ans, it compiles
Yet, in both cases, we aren't accessing anything different, so what is going on? It appears to fail to compile if the associated type is used in a trait def, but if used in an impl block, it succeeds.
So why is it happening? (regardless of whether this is a good idea to use this in my code, I just stumbled into it, and want to understand why it's happening, while I'll remove the dependency on the sealed trait associated type later on)