trait MyTrait<T> {
type Output;
}
impl<T> MyTrait<T> for T {
type Output = T;
}
fn show<T: MyTrait<T, Output = T> + Default>() -> T::Output {
T::default()
}
fn main() {
let i: i32 = show();
}
In this example, even thought I have implies that T::Output
is T
in the trait bound T: MyTrait<T, Output = T>
, however, the compiler cannot infer type for T
. If I change the return type to T
, the compiler can infer T
to i32
. In contrast, the compiler can infer FnTrait::Output
, for example:
fn infer_output<F,U:Default>(_:F)->F::Output
where F:Fn()->U
{
U::default()
}
fn function<U:Default>()->U{
U::default()
}
fn main() {
let i:i32 = infer_output(function);
}
Similar to the first case, the trait bound F:Fn()->U
implies that F::Output
is U
, and the compiler can infer U
to i32
as what is expected.
I think the compiler should enhance the type inference for the first example to make the type inference behave the same as that of the second case.