Hey, I'm trying to return two different types in one function, in OOP normally i utilize polymorphism.
Example, a have a simple interface or an abstract class and an instance that implements that interface, so, I can have two types that implements that interface.
Pseudo Code
public interface base {
public void something();
}
public class One implements base {
public void something() {}
}
public class Two implements base {
public void something() {}
}
public class Operation {
public base test() {
if (true) {
return new One();
} else {
return new Two();
}
}
}
class Playground {
public static void main(String[ ] args) {
base base_one = new One();
base base_two = new Two();
Operation test = new Operation();
base obj = test.test();
obj.something();
}
}
In Rust, the closest I got is trait object, but... I can't use a trait that contain a Self. The error is on function instance()
I know that the rust way is composition over inheritance, but how do you translate an old code base in OOP to Rust?, I have some projects that would like to make in Rust, but sometimes a I get stuck, is not simple to redefine an architecture. Anyway if you know a material that explain how to make x in y lang to rust. Sorry for the bad English.