In my experience, nobody really knows what OOP is, everyone has a different definition.
Most of the “OOP” features are implemented in languages that are not OOP. The only one that is to me an OOP-exclusive feature is class-inheritance. So IMO OOP=class inheritance.
There is plenty of criticism about inheritance, specially among rust lovers. Since rust implements other features associated with classes, except class inheritance. Such as: methods (and self keyword), interfaces (traits), default interface method implementation.
Anti-OOPs usually argue that encapsulation and interface is a much better alternative to class inheritance.
Some things class inheritance is criticized for:
Diamond inheritance problem: If there is class A. B and C inherit from A and override its methods. D inherits B and C without overriding them. What implementation should D inherit? B or C? Same happens if only B or C overrides.
Encourages having multiple layers of abstraction: it’s not uncommon to see huge inheritance chains. MyCustomList -> OrderedVector -> OrderedList and Vector -> List -> Collection -> Iterator. Just by looking at MyCustomList, you don’t know the entire chain, you just see “OrderedVector”. You have to follow many nested links until you can know it all, and then you have to retain that knowledge along with tens of other inheritance chains.
Not ideal for performance: Inheritance encourages designs where the compiler will need to add a v-table to classes. These tables make implementation of OOP patterns much easier, but they require additional overhead when calling methods. Note that v-tables are not OOP specific, rust needs them also for trait objects. However, rust encourages designs with small amount of trait objects.
Not as intuitive as claimed: People are taught OOP with simple examples involving real-world objects like: car -> vehicle -> object. However, these situations are rare except in some specific cases like UIs, video games, simulations. In most other cases, you are dealing with concepts rather than objects. And even when you’re dealing with objects, it’s not a clear cut. Sometimes it might happen that bicycle -> car. Even though not intuitive, in some situations this may be a useful inheritance. But when bicycle inherits car, it no longer resembles the inheritance-chain of the real world, so that’s extra work for the brain.
I hate both of them. The first one is very clunky with all the
"
. The second one is not self-docummenting at all, and it makes some enums impossible.For example, you can’t represent:
enum A { B(u32) C(u32) D }
It would be
A { | u32 | u32 | () }
Also, the pipe is very awkward to type, specially depending on keyboard layout. Since it’s a rare character. If you need to separate between enums and struts and really don’t want to use the
enum
andstruct
keywords, you can use different delimiters, like:A [ u32, u32 ] B { u32, u32 }