5 ms·
How do you write polymorphic code without inheritance?
by ythn 9y ago
How do you write polymorphic code without inheritance?
- kcmastrpc 9y agowith interfaces? example: see golang
- falcolas 9y agoYou use a language that lets you use methods and members of objects without requiring a specific class. Java does this via interfaces, Ruby and Python by not typing the parameters.
- ythn 9y agoSometimes "use another language" is not a viable option. We are mainly an embedded c++ shop and switching to Java is not an option.
- frou_dh 9y agoInterfaces give polymorphism. Implementation inheritance gives method reuse. If you think about the term polymorph ("many shapes"), that describes the world of differing objects that have the opportunity to satisfy an interface.
- ahartmetz 9y agoThat's a nice way to think about it. One of the things that still slow me down sometimes when designing something in C++ is its somewhat muddled concepts. The overlapping concepts make it harder to divide and conquer the problem to solve. Rust is quite different regarding interfaces and benefits from many more years of research and experience, so I have reason to assume it does better.
- sanderjd 9y agoFWIW: Java is a language that is more of a contemporary of C++ but gets this much more right. While earlier Java code I've seen seems more like most C++ in the muddiness of the lines between polymorphism and code re-use, most newer Java code I see these days has very flat inheritance hierarchies (if any at all), and makes far more broad use of interfaces and composition. My point is just that it's not only "modern" languages like Rust that have better support than C++ for this distinction.
- marcosdumay 9y agoJava is a decade younger than C++. Also, I haven't ever seen deep C++ hierarchies. That seems to be invented for Java.
- HelloNurse 9y agoProbably you have only seen good and/or recent C++ code. Deep inheritance hierarchies are easy and cheap in Java (in fact, Java is deliberately designed to be good at them), but their popularity is older and wider; historically the early development of complicated OOP took place mostly in C++, with Java attempting to facilitate further complication with more modern tools. For a well-known (and not too unreasonable) example of C++ deep class hierarchy, see the streams in the standard library of 20 years ago. The Java standard library copies them as closely as possible.
- marcosdumay 9y agoIn retrospect, it's indeed very likely that I have only seen good C++ hierarchies.
- mitchty 9y agoYou need to quantify what you mean by polymorphic. Do you mean sub typing, ad-hoc, or parametric polymorphism? The latter requires no inheritance.
- maxxxxx 9y agoIs polymorphism really needed for OOP?
- dragonwriter 9y agoWhat is done in a typical OOP language with inheritance you can also do with interfaces (assuming the language has them) with more implementation duplication, and/or with mixins (like Ruby modules) without that kind of duplication (the latter can be seen as a kind of inheritance and may be ruled out by some objections to inheritance.)
- AdieuToLogic 9y agoYou mentioned being "an embedded C++ shop" in another comment, so I'll frame this in C/C++. One way is to assign function pointers within a struct which is shared across top level functions. This is very common in C, such as in the standard i/o library. It is also very handy in dynamic module initialization in both C and C++, such what Apache httpd supports. In-process C++ polymorphism of course can be done the same way, but using classes is the idiomatic approach. An interesting and highly useful form of polymorphism in C++ is tag dispatching[0] in templates. This can be complemented with usage of traits to achieve very flexible implementations. A book I highly recommend which discusses these techniques as well as C++ templates in detail is "C++ Templates The Complete Guide"[1]. 0 - http://www.boost.org/community/generic_programming.html#tag_dispatching http://www.boost.org/community/generic_programming.html#tag_... 1 - https://www.textbookrush.com/browse/books/9780201734843 https://www.textbookrush.com/browse/books/9780201734843
- Betadel 9y agoIn addition to what others have already mentions, you can use tagged unions as an alternative of subtype polymorphism., for example with std::variant.