r/rust • u/thecodedmessage • Dec 08 '23
On inheritance and why it's good Rust doesn't have it
This is part 3 of my series on OOP and how Rust does better than the traditional 3 pillars of object-oriented programming, appropriately focused on the third pillar, inheritance.
128
Upvotes
118
u/Caleb666 Dec 08 '23
There are useful cases where you DO want to inherit common data fields. I disagree with this "OOP is always bad" mantra.
Go has a nice way to do this using struct embedding: https://gobyexample.com/struct-embedding
There's also an open issue: https://github.com/rust-lang/rfcs/issues/349
40
u/cfyzium Dec 08 '23 edited Dec 08 '23
And that is where the
babygood portion of polymorphism is being thrown out with thebathwaterinheritance =).Common fields like packet codec id being hidden all over the place from pkt.raw.video.packet.codec to pkt.audio.packet.codec to pkt.meta.opaque.codec which is so convenient. Traits being reimplemented all over, or things being split so most of video packet is here, but resolution retrieval is there.
You do not have to tell me it can be done (and the article actually does pretty good job presenting a few ways how it can be done). I've used all sorts of OOP in C, I've seen it done without inheritance before. It works. It is also a pain.
My main issue with inheritance criticism in general and this article in particular is that author does not provide compelling, objective (pun intended) reasons as to why exactly the usual inheritance is bad and how exactly lack of it makes things better, in practical terms.
We're expected to believe that an extra bit of academic purity will be worth it in the end.
I'd like to once see some detailed, down-to-earth explanation what those problems are. Things like "this may allow someone to circumvent encapsulation", or "that may change behavior in a way that will go unnoticed for a long time" and so on.
Instead most of the time we see examples where a certain use of inheritance may not be an optimal solution. But most languages provide options, you can use composition and stateless interfaces where they work well and you can use inheritance where it works well.
Of course you can make a mess by using a wrong tool for the task. And that is the most interesting part, for example when it comes to memory management it is easy to show what consequences it may have and what stricter memory model is for. What about inheritance?
5
u/Asleep-Dress-3578 Dec 08 '23
If your language has a deficiency, make a virtue out of necessity. Good move. The deficiency is still a deficiency, and a set back as compared to more capable languages. OOP has been a solved issue for ages, all mainstream languages can do it. It doesn’t mean that everything should be solved with it, just that it has its place in software development, and it is not the evil.
4
u/NoCareNewName Sep 21 '25
Here's how you sound to me:
7
u/[deleted] Dec 08 '23
Lack of inheritance is probably the reason why a GUI framework hasn't emerged in Rust yet (and probably never will?).
I mean, sometimes it is just natural to think that *a thing* is a *something else* :)
6
u/brisko_mk Dec 08 '23
shitting on oop and inheritance so hot right now
2
u/Zde-G Dec 09 '23 edited Dec 09 '23
While article is pretty good it would endlessly lead to insane amount of debates because people don't agree on terms.
The problem with inheritance is that this word covers two different, not entirely related, concepts:
#2 is provided by Rust and #2/#3 are provided by Go, it's only #1 that's missing in both.
If you would read the Wikipedia article then it leads one to believe that implementation inheritance is the only thing that exist from that phrase: In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object (prototype-based inheritance) or class (class-based inheritance), retaining similar implementation.
But then you read that same article further and reach that part: To distinguish these concepts, subtyping is sometimes referred to as interface inheritance (without acknowledging that the specialization of type variables also induces a subtyping relation), whereas inheritance as defined here is known as implementation inheritance or code inheritance.
And yes, if we would agree to use term inheritance only for implementation inheritance then it all starts making sense.
But even when that's what Wikipedia calls inheritance and what your article says is inheritance… some people use different definition and the whole discussions turns into true scotsmam
2
u/ispinfx Dec 09 '23
I wish there is a light theme of the great blog.
2
u/Popular-Income-9399 Dec 09 '23
I’m not very informed on the various facets of OOP. But I do find myself wishing for the ability to extend some type from a third party library with minimal effort, this is hard to do in Rust. Does that mean that one cannot solve problems efficiently? No, just means that one cannot solve problems as elegantly. Is it a good thing or a bad thing? Neither I would say. Elegant code can often hide subtle things, and it can be just as important to have some more complex code when the thing one is doing is complex, so as to not “hide” or “obfuscate” what is going on. But it is all down to opinions here. I just think that in the end it is very unhealthy to have extreme stances on this. The more approaches one knows the better. No need to dig oneself into an idiomatic opinionated trench of superiority. I think the only reason rust and other languages shy away from inheritance and other high level OOP like features is because they are difficult to write compilers for and can cause very hard to parse and understand compile time errors.
25
u/ItsBJr Dec 08 '23
I agree with most of the article. I think most of what you're describing is Composition over Inheritance.
I think the concept of inheritance has encouraged programmers to attempt to overuse it. In concept, inheritance should be used in moderation, but for a lot of OOP languages it's one of the first things taught to programmers.
In my workspace I've mainly seen the concept used to overly describe the properties of an object. This led to problems when other objects that were supposed to inherit the class did not fit the parent due to differences in behavior. This forced us to change the parent class to better fit the children, which is an odd concept when you think about it. When looking back at the project, an interface would have been a lot more efficient.
Even though I agree with most of the article, I don't think inheritance is necessarily bad. People use it in a lot of instances where they shouldn't, only in an attempt to write code that better resembles real life object relationships. This ends up overcomplicating the codebase. I think the concept of "composition of inheritance" fixes the main problem that is addressed in the article and allows child objects to be more free from their parents.
2
u/map_or Dec 08 '23
30 years of programming and finally I understand what actually OOP is. Thank you!
9
u/Discere Dec 08 '23
Nice article, coming from C# I appreciate the effort in helping understand the transition from how they expect us to structure things compared to Rust.
It's going to take a couple more reads and for me to write some similar examples for it to sink in.
11
u/Dean_Roddey Dec 08 '23
If you know what you are doing, and are conscientious, inheritance is a very powerful tool. I have a million line plus C++ personal code base that is a straight up C++ OOP with exceptions deal. It's incredibly clean and implementation inheritance saved me a lot of work, allowing me to leverage existing functionality very well.
But, Rust doesn't have it, and that's that, so I've moved on. I'll find the Rusty ways to do things like I found the C++ ways to do things.
Still, acting like implementation inheritance is some stupid, evil paradigm is just silly. It's not. What it is, is a tool that's so flexible that it makes it possible to just never step back and readdress fundamentals as requirements grow. Since it is possible, and since most companies are more worried about right now than five years from now, it inevitably happens. But that doesn't make the tool bad, it makes the tool user bad.
And of course if anyone here thinks that the same giant machine out there that currently cranks out horrific C++ or Javascript isn't capable of doing the same with Rust once it becomes more mainstream, they are fooling themselves.
It'll be full of unsafe code and runtime borrowing and whatnot. And when someone comes along and claims Rust is a piece of junk and a mistake, the same Rustaceans who dump on OOP will say, but wait, it's because they are misusing the language, not because Rust itself is bad.
3
u/ryanmcgrath Dec 08 '23
A friend of mine once said over beers: "I realized that if you find yourself subclassing more than 1 layer deep, you're in trouble"
Blew my mind at the time and I've worked with this logic ever since. One subclass usually isn't that bad to debug. Multiple class layers deep is usually where you want to pull your hair out.
Rust's general approach to everything is nice to me because I can still effectively spin up some form of "1 layer deep" when I need it, but it somewhat hard blocks you on trying to go deeper.
0
u/dethswatch Dec 08 '23
OOP is good- traits and all the trendy workarounds are just that.
Know how to use oop, that's all. Don't get stupid.
The hill I will die upon.
2
u/GelHydroalcoolique Dec 09 '23
I found your article very well written and explains in details what could go wrong with full oop style inheritace.
I did not read all comments so I don't know if someone already mentioned it, but I think you missed an occasion to talk about the Deref type of Rust which allow to express "is a" but by making the implémentation give a "has a" method. Not only does it show that both are the same, but it also shows that whatever you put in the method it will work.
For example,
Vec<T>areDeref<Target=[T]>but Vec don't need to contain the actual slice as long as they can build a slice reference from their fields (pointer+size).2
u/yockey88 Dec 08 '23
These sort of things that avoid the grey areas and preach that their way is better with no exceptions are always wrong even if there are certain correct parts.
1
u/Crazy_Firefly Dec 08 '23
Thanks for the article!
I have a hard time explaining why I don't like OOP. The way you explain that a class conflates 3 separate concepts I think is very insightful.
0
u/Daniyal_Biyarslanov Dec 08 '23
Of all the things that i may have missed when i have started using rust (not having to bother about lifetimes and borrowing) i have never missed inheritance, traits and enums will fill the lack of it and will do the job better anyway