Hi everyone! When i try to compile this, compiler consider associated type here &ServiceTypesImpl::UserService as ambigious and advice me to use fully qualified syntax to upcast concrete type to trait, so i can fix this, but i cant understand why it considered as ambigious?
There isn't anything to indicate where the associated type is coming from. If you added another trait with an associated type with the same name, there's now two possibilities:
pub trait AnotherAssociatedType {
type UserService;
}
impl AnotherAssociatedType for ServiceTypesImpl {
type UserService = ();
}
help: use fully-qualified syntax
|
25 | fn user_service(&self) -> &<ServiceTypesImpl as AnotherAssociatedType>::UserService {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 | fn user_service(&self) -> &<ServiceTypesImpl as ServicesTypes>::UserService {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You could say the trait declaration has the information to disambiguate this, but impl blocks are supposed to make sense by themselves, for human reasons.
Yeah, except when the type is Self and the associated type is from the trait being implemented. Usually you'll just be copying these from the definition anyway.
impl Iterator for Collection {
type Item = i32;
fn next(&mut self) -> Option<Self::Item> { todo!() }
^^^^^^^^^^
}