Sunday, 5 January 2014

Yet another post about Duck Typing

"If it walks like a duck, and quacks like a duck, it’s probably gonna throw exceptions at runtime."

The Three Articles

The first article in my rss reader I ignored. I know what duck typing is, so I don't read a post about it. Seeing the second title I raised my eyebrow. But when a third one appeared on the list I decided I need to read them: I don't follow fools' blogs, so three posts about the topic clearly means that something is worth a look there.
Eric Lippert expresses his confusion over the Wikipedia page about Duck Typing. I skimmed his article: he's pointing out some weaknesses of the Wikipedia's definition. Nothing interesting here. There are facts and there are community-edited articles. Let's move on and see what the other two posts are about. As I expected, those are reactions to the first one. Still nothing interesting... except! Except that Glenn Block calls Eric Lippert (the author of the original article) "one of the fathers of C#". Okaaay. So either The Fathers Of C# have no idea about the basic definitions of programming type systems and the IT industry is doomed, or I missed something. At this point I re-read Eric's post properly and a long journey's began.

My Two Pennies

I have no two pennies today. After reading some more articles and a research paper I still don't have a final conclusion. Here are three vague definitions of duck typing I have after all this reading:
"Structural Typing may be thought of as a kind of compiler-enforced subset of Duck Typing." (by Mark Rendle in the comments of Eric's post)
"Duck typing can be seen as the run-time, dynamically typed equivalent of structural typing" (from the publication above)
"Structural Typing which provides a way to do Duck Typing in a compiler safe manner that works with static types" (by Glenn Block)
The definition of early binding/late binding (or static typing/dynamic typing) are clear just as structural typing is. Duck typing is the only one that just doesn't feel like a well-defined entity but it still feels like "something": Python and JavaScript clearly have duck typing. And the C# dynamic keyword feels like it is not duck typing, but Glenn Block, among others, disagrees with this. And I myself also disagreed one hour ago. Occam's razor to the rescue: probably duck typing is not different from late binding. I accept this until I see a late binding that clearly is not duck typing.