2 ms·
Delegation is a very useful part of composition. Almost all OOP languages have two techniques to delegate some methods to another object: - manually write a bu
by pjc50 1y ago
Delegation is a very useful part of composition. Almost all OOP languages have two techniques to delegate some methods to another object:
- manually write a bunch of forwarding methods and remember to keep them updated, or
- inheritance.
- vips7L 1y agoThere’s also automatic delegation like in Kotlin: https://kotlinlang.org/docs/delegation.html https://kotlinlang.org/docs/delegation.html
- bccdee 1y agoTo be fair, the compiler generally forces you to keep the forwarding methods updated. It can be irritating, but there's little risk of forgetting in a statically-typed language. Manual forwarding also operates as a forcing function to write small interfaces and to keep different pieces of logic separated in different layers, both of which feel like good design to me. (Though I'm not saying I'd turn my nose up at a terser notation for method forwarding, haha.)
- LukeShu 1y ago...yes? Hence me saying that 'composition' and 'polymorphism' have often been unnecessarily coupled together in 'inheritance'? Compare: Ruby mixins or Go embedded struct fields.