How to deal with the lack of struct inheritance in Mojo

As we don't have struct inheritence in Mojo yet, is the use of Dependency Injection the way to go? Or what other approaches should i consider. I know this is a very general question but i am actual looking for some general advice here. I try to port a python project to Mojo, but as this code makes use of (class) inheritance i need figure out how to approach this - or if i better wait for Mojo to introduce inheritence ... Thx
10 Replies
Melody Daniel
Melody Daniel2mo ago
If it can not be rearchitected then you have to wait for Mojo to introduces classes
Martin Dudek
Martin Dudek2mo ago
yes that is basically my question to the software architecture gurus here, what approaches to re-architecture class inheritence based software are worth looking into
bunny
bunny2mo ago
Composition over inheritance
Composition over inheritance (or composite reuse principle) in object-oriented programming (OOP) is the principle that classes should favor polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) over inheritance from a base or parent class. Ideally all reuse ca...
Martin Dudek
Martin Dudek2mo ago
thanks a lot@bunny, these type of information i was looking for
bunny
bunny2mo ago
You're welcome! So, I spent years programming with a purist OOP mentality:
Everything is an Object and Inheritance is awesome!!
Then, I started encountering Inheritance Hell. https://www2.cs.arizona.edu/~mercer/Presentations/OOPD/InheritanceHellReading14.pdf The worst was a company I consulted at where they took some inspiration from the precepts of "keep stuff small." So, they made every Class contain no more than 2 or 3 new methods. Then, they'd build higher classes by inheriting from three or four smaller classes. Keep going up the food chain. And, many classes used the same method names (overwriting each other). I was helping them debug something that involved a class with 14 levels of inheritance under it (at the deepest, DFS-style) and a total family tree of substantially over 100 inherited classes. .......I did not communicate like a professional that day -- F-bombs were dropped on the regular. 😂
Anyway, I still use inheritance, but in very limited scenarios and only if the client prefers it. I've become a pretty fond of "composition over inheritance." And many of the most popular newer languages (such as Go and Rust... and Mojo) avoid inheritance, preferring Traits, Interfaces, and other means of expressing code. I will admit that my tiny little brain still tends to think in classic Inheritance architecture -- it's a hard habit to break when it's all you did for a loooooong time. But I really do prefer composition and I am slowly rewiring my thinking. Perhaps you will too. ❤️ I should add that Mojo's roadmap does state that Pythonic Classes will come down the pipe. So people will probably create Inheritance Hell in some Mojo code, too. But hopefully having exposure to more composition-focused thinking will convert more legacy Python devs (like me) toward different code architectures.
Martin Dudek
Martin Dudek2mo ago
great, thanks for sharing your process to rewire. For me like i guess for many, inheritance was just the cool thing when starting with OOP. But its probably a blessing that we don't have it yet for structs in Mojo 😉 really very helpful what you shared, and happy now to look into composition ....
bunny
bunny2mo ago
@Martin Dudek , this vid is really solid, too. It popped into my Youtube feed today (I play it as random background) and I immediately thought "I gotta share this." 🙂 https://www.youtube.com/watch?v=hxGOiiR9ZKg Also, The Primeagen had a take on that vid. ...of note: some people feel that Prime is a bit salty in language use, so depending on where you work, don't watch this vid at work: https://www.youtube.com/watch?v=HOSdPhAKupw
CodeAesthetic
YouTube
The Flaws of Inheritance
Let's discuss the tradeoffs between Inheritance and Composition Access to code examples, discord, song names and more at https://www.patreon.com/codeaesthetic 0:00 Introduction 0:25 Inheritance 3:32 Composition 5:22 Abstracting with Inheritance 6:52 Abstracting with Interfaces 8:20 When to use Inheritance
benny
benny2mo ago
+1 for that video and the channel as well, one of my favorite code youtubers
bunny
bunny2mo ago
Facts! Code Aesthetic is 100% gold. ...and I like Prime a lot too. He makes me laugh, and we can all use more laughs in our day. 😂
Martin Dudek
Martin Dudek2mo ago
very clear, thanks a lot. Didn't know this YT channel... subscribed