3 ms·
The only real difference is that inheritance as explicit language support while composition is something you can easily implement with common language features.
by fabian2k 4y ago
The only real difference is that inheritance as explicit language support while composition is something you can easily implement with common language features. It's not really more difficult to use composition than inheritance.
As an example, if I want to use composition in C# using ASP.NET, I simply inject the class I want to use into my new class using the built-in DI. This is as simple as extending the class.
- substation13 4y agoSome features are plain missing (or must be implemented in a convoluted manner): - Discriminated unions - Immutable data types (records) - Free functions (or modules of free functions) - Software transactional memory (STM), atoms - Ad-hoc polymorphism (traits) - Custom operators (used sparingly for building DSLs) - List comprehensions Instead we get anti-features like automatic properties, implementation inheritance, events, object initializers, etc...
- tester756 4y ago>(records) there are >- List comprehensions Enumerable.Range? >- Custom operators (used sparingly for building DSLs) Could you please show what kind of DSL you'd want to write?
- substation13 4y ago>(records) Agreed, although this a recent addition in Java and a step away from old school OOP >- List comprehensions Enumerable.Range is not equivalent to list comprehensions. For example, in F# you can build a list using a comprehension containing more or less any code you like. It's incredibly powerful for things like building HTML. >- Custom operators (used sparingly for building DSLs) These are used in many places, but to give one example https://www.cs.tufts.edu/~nr/cs257/archive/simon-peyton-jones/contracts.pdf https://www.cs.tufts.edu/~nr/cs257/archive/simon-peyton-jone...