3 ms·
Rust has a flavor of inheritance through Traits and #derive.
by queuebert 4y ago
Rust has a flavor of inheritance through Traits and #derive.
- zozbot234 4y agoTraits implement interface inheritance, yes. #derive has nothing to do with derived classes in OOP, it's an annotation-like facility that adds auto-generated code to any object.
- queuebert 4y agoIsn't #derive a syntactical sugar for a bare impl?
- alerighi 4y agoNo, traits are not inheritance, nor is derive that is default implementation of some traits. Traits are like interfaces in OOP languages (simplified), the equivalent of a class would be a struct + all the impls of that struct. And that thing cannot be extends, a struct is fixed when defined and nobody can extend it, you can use composition that is the correct to me way to go.
- queuebert 4y agoThat's why I said "flavor". It's not exactly inheritance obviously, but provides some of the same functionality. There are many ways to skin an OO cat. Traits describe the methods implemented, and you can override with specific implementations or inherit the default, from a level up.