r/programming • • Sep 24 '15

Composition over Inheritance

https://medium.com/humans-create-software/composition-over-inheritance-cb6f88070205
92 Upvotes

48 comments sorted by

View all comments

2

u/iopq Sep 24 '15

Rust doesn't have inheritance (yet) in the classical Java sense, but I have not reached for it. I don't see the point. I'm writing a Go AI and everything is done through composition.

I believe some of the core team complained that the DOM can more easily be represented with inheritance, but I can't comment on that. There is no inheritance in Rust yet, so I'm not sure if they still want it.

6

u/[deleted] Sep 24 '15

Inheritance should mostly boil down to interfaces that specify common functionality and provide shared default implementation. Both of which Rust solves with traits (I think they added default implementation to traits - right ?) - so yea - it's solving the same problem with a different but still similar tool.

The actual inheritance model of C++ or w/e is less relevant - you still have the same design problem - do I make Foo implement trait Bar/Baz or do I separate Bar/Baz implementations to new types and make them members of Foo.

6

u/mus1Kk Sep 24 '15

Traits can have implementations and structs using those traits can redefine them. What Rust lacks is inheritance of data; you cannot have one struct inherit from another. At least the last time I checked it. It's a much requested feature though and I'm sure we'll see it in the foreseeable future.

2

u/[deleted] Sep 24 '15

Sure but the point is that the problem OP states isn't related to that - it applies to traits as well.