3 ms·
For me, one of the main reasons to use OOP is dynamic dispatch / runtime polymorphism. I have not spent much time with Rust, but it seems like that is a bit cum
by captainmuon 4y ago
For me, one of the main reasons to use OOP is dynamic dispatch / runtime polymorphism. I have not spent much time with Rust, but it seems like that is a bit cumbersome there, and doesn't exactly work like you would expect from Java, C++, Python, ....
I mostly use OOP when I have a bunch of Foos and Bars, and want to treat them differently in some places. Instead of having ifs in each function, I use inheritance, and override the methods that I want to treat differently. It's mostly about heterogenous containers and avoiding explicit ifs. When you use it that way, it's safe and convenient and I've never ran into the fragile base class problem or other typical issues.
- pentachoron 4y agoNot a Rust expert but: doesn't Rust support this use-case via Box<dyn SomeTrait> , and leveraging default Trait methods, only overriding the defaults you want to for each type implementing the Trait?
- nicoburns 4y agoIt definitely works differently to C++/Java/Python, but IMO this is one place where Rust is a huge improvement. You are effectively always generic over an interface (trait in Rust) rather than a base class, and this is much more flexible as a given strict can implement as many interfaces as it likes rather than being stuck in one class hierarchy (or having to deal with the pain of multiple inheritance)
- pjmlp 4y agoIt is no different than using interfaces, protocols, pure virtual base classes....
- dllthomas 4y agoIt's similar, but there are some differences. Notably, my package can implement my trait for your type, while my package (usually? always?) cannot make your type inherit from my virtual base class.
- pjmlp 4y agoDepends on the flexibitly of the language, there isn't a golden way to do that across all variations of OOP.
- eklavya 4y agoWrapper classes should be equally good once Valhalla (jdk) lands, without introducing another pointer indirection.
- adamc 4y agoInteresting but Java gets more and more complex in a way that does not make me want to write things in Java.
- Scarbutt 4y agoWhat you describe can be achieved in Java via composition (instead of inheritance) and using interfaces.