hey hello,
i would love to know if there is another way to decouple a function from it's output type
dynamic dispatch sim to work well, but if i can evaluate statically at compile time i would not say no... any idea? thx !
what i simply want is returning an abstract trait instead of concrete type ?
fn main() {
let a = produce_output(true);
let b = produce_output(false);
println!("a->{}", a.traduction());
println!("b->{}", b.traduction());
}
struct data_to_spanish {
raw_txt: String,
}
impl traductible for data_to_spanish {
fn traduction(&self) -> String {
"el zorro marrón rojizo salta sobre el perro perezoso".to_owned()
}
}
struct data_to_french {
raw_txt: String,
}
impl traductible for data_to_french {
fn traduction(&self) -> String {
"Le renard brun roux saute par-dessus le chien paresseux".to_owned()
}
}
trait traductible {
fn traduction(&self) -> String;
}
fn produce_output(input: bool) -> Box<dyn traductible> {
if input == true {
Box::new(data_to_french {
raw_txt: "the red brown fox jump over the lazy dog".to_owned(),
})
} else {
Box::new(data_to_spanish {
raw_txt: "the red brown fox jump over the lazy dog".to_owned(),
})
}
}