Live data from Hacker News

Message Passing, Duck Typing, Object Composition, and not Inheritance

fitzgeraldnick.com

11–14 of 14 posts

Re: Message Passing, Duck Typing, Object Composition, and not Inheritance

#11

Inheritance is two language features in one though—It's implicit delegation and type polymorphism. Those two features need not be coupled together like that, and in fact, shouldn't in my opinion. It's not that silly things can be done with inheritance, it's more that people often use inheritance for one of two things, but rarely both: implicit delegation and type polymorphism. Pick your poison, and use the appropriat…

You express the situation quite well here. Are there any languages that do separate implicit delegation and type polymorphism?

As mentioned by ericbb, there is Go, yes. I've been working on another; a prototype based OO language without inheritance, explicit delegation and type polymorphism, but have nothing I want to show just yet.

Re: Message Passing, Duck Typing, Object Composition, and not Inheritance

#12
post #3

Sometimes, inheritance is the right tool for the job. Sometimes, composition is the right tool for the job. Sometimes, duck typing is the right tool for the job. The only valid absolute is that bloggers that insist otherwise are wrong.

Indeed. I recently replaced a containment relationship with an inheritance relationship. Sometimes inheritance does make sense yet despite it being the least-bad "tool for the job", it seems awkward, fragile and ill-thought-out. You have some attributes hidden in the parent and attributes right there in the child. The idea that the ontological relationships "is-a", "has-a" etc should map cleanly to the class as bucke…

"it seems awkward, fragile and ill-thought-out"

Or, you know...not. Sometimes, inheritance is the perfect abstraction for the job, neither awkward nor fragile, but perfectly elegant. Which was my point.

When you've got an is-a relationship, inheritance often makes sense, and encapsulation is frequently awkward. When you've got a has-a relationship, inheritance is frequently awkward. Just because you can find an awkward use of inheritance or a leaky abstraction doesn't mean that inheritance is awkward. It's a tool with limitations, and the world is a messy place. Containment isn't a magic bullet, either.

I honestly don't understand where the bias against concepts comes from. When the concept works, you use it. When it doesn't, you don't. It's engineering, not religion.

Re: Message Passing, Duck Typing, Object Composition, and not Inheritance

#14
post #3

Sometimes, inheritance is the right tool for the job. Sometimes, composition is the right tool for the job. Sometimes, duck typing is the right tool for the job. The only valid absolute is that bloggers that insist otherwise are wrong.

I sympathize with the idea behind your post but it's really hard for me to think of a good usage of duck typing, or even structural typing. Here are a few reasons why:

http://goo.gl/7dL3a

Post reply on HN