Thursday, October 9, 2008

On Ducks...

All things in my world seem to be pointing toward ducks lately. I just unlocked the "Quacker" bike in Mario Kart Wii, I got a random sales call from Black Duck Software, and I stumbled upon the concept of duck typing for the first time. All on the same day. I've always been a firm believer, as Red Green often attested, that duct tape is indispensable. But now I'm starting to wonder whether all those years he was saying "duck type". If duct tape is a handyman's secret weapon, should duck typing be a programmer's secret weapon?

So what is duck typing? It's the notion that if something walks (waddles) like a duck and quacks like a duck, then it can be assumed to be a duck. Duck typing, commonly used in languages like Ruby and Python, is a style of typing where an object's current set of methods and properties, rather than its inheritance from a particular class, determines its semantics. Duck typing really isn't all that new - it's really about dynamic versus static typing. Real programming languages like Smalltalk have supported this concept for a long time. ;)

Who decides what the definitive characteristics of a duck are? Does something need to look like, dress like, or weigh as much as, a duck to be a duck (or a witch, as the case may be)? Does it have to feel, smell, and taste like a duck to be a duck? If "beauty" is in the eye of the beholder, does the beholder get to decide what a duck is? Is it a matter of perception?

After reading about duck typing, I got to thinking that it might be one approach to resolving the issue of unintended inheritance in UML. What if, rather than specializing metaclasses from a common infrastructure, or merging them into a hybrid representation using package merge, related languages could simply declare what they assume the definitive set of shared characteristics for each metaclass (duck) is? Should a metamodel perhaps be simply a selection of mixins rather than a complex heirarchy of (base) classes?

5 comments:

AlBlue said...

Is SmallTak a cut down version of that popular language, SmallTalk?

Wayne said...

>Who decides what the definitive
>characteristics of a duck are?

The caller decides. If the object responds to the messages I need to send, then it's a duck. Or whatever. We never called it "duck typing" in the "old days", we just wrote code that worked.

Alex, FWIW, "Smalltalk" is one word, not two (sorry, I get a little emotional when Smalltalk is the topic of discussion).

Kenn Hussey said...

Oops, thanks for catching the typo; it's been fixed.

Ed Merks said...

Kenn, we really need to bring back the practice of witch hunts. It's such a great community building experience, except of course for the witch...

Cory Casanave said...

The “duck” scenario you outline is also what is used in OWL & RDF (The Semantic Web Standards) as well as First Order Logic. It has also been used in multiple implementation frameworks which allow “clusters” of OO language objects to represent the same concept. James Odell wrote a great article on dynamic multiple classification here: http://www.jamesodell.com/Dynamic-multiple-classification.pdf - note this was published in 1992. Breaking the single classification bottleneck may well be the most important transition OO systems can make to support a more dynamic world.
In some of our own protypes we have used RDF as the MOF implementation so that we could get these more dynamic features.