I am pretty sure that if Rust got anonymous types, the signature of foo
would be something like.
fn foo(cond: bool) -> (i32 | &'static str);
Which open some really interesting design idea:
fn foo(cond: bool) -> impl Debug {
// Automatically create an enum with two variant, so the value returned
// by both branch of the `if` don't need to be the same.
// Anonymous enum would implements all the trait implemented by all
// the variants, so this anonymous variant would implement `Debug`.
let ret: (i32 | &'static str) = if cond {
42
} else {
"foo"
}
ret
}
And the compiler should probably at this point be smart enough to understand this:
fn foo(cond: bool) -> impl Debug {
if cond {
42 as i32
} else {
"foo"
}
// the type created by this `if` statement is an anonymous enum
// of (i32 | &'static str). Both variants implements `Debug`, so the
// anonymous enum would also implement `Debug`.
}