Open
Description
Hi! I was surprised to see that in the following code, explicit return type (or map
turbofish) annotation has to be specified for the map
combinator for the code to compile:
struct A;
struct B;
trait Trait {}
impl Trait for A {}
impl Trait for B {}
fn foo(arg: bool) {
// Compiles
let _: Option<Box<dyn Trait>> = match arg {
true => Some(Box::new(A)),
false => Some(Box::new(B)),
};
// Doesn't compile
/*let _: Option<Box<dyn Trait>> = match arg {
true => Some(()).map(|v| Box::new(A)),
false => Some(()).map(|v| Box::new(B)),
};*/
// Compiles
let _: Option<Box<dyn Trait>> = match arg {
true => Some(()).map(|v| -> Box<dyn Trait> { Box::new(A) }), // or Some(()).map::<Box<dyn Trait>, _>(...)
false => Some(()).map(|v| -> Box<dyn Trait> { Box::new(B) }),
};
}
The inference can work it out if I construct the Option directly, but once map
is added, it cannot see that both of the map
s should coerce to Box<dyn Trait>
. Is this a limitation of the current type inference or should this work?