From: Aredridel Date: 2004-06-06T10:53:31+09:00 Subject: Re: How to ducktype a Hash? > Duck typing cannot work unless methods with the same names and > signatures behave in the same way in the various classes that implement > them. If the ability to do duck typing is deemed to be an important and > necessary feature of an object-oriented language, then the standard > libraries that are written for that language _must_ provide different > names and/or signatures for methods that perform different functions. I'd say I agree and disagree. Duck typing is more like a powerful tool: You can cut yourself easily, but you can do things with it that you cannot otherwise. What you speak of is interface typing or protocol contracts. That's another matter. Duck typing is mostly absence of checks, so if you feel the need to make an array-like class, and pass instances to something that expects something relatively array-like, you can. It's something that takes both care and good design. If something is inherently simple, duck typing is powerful and useful because it does nothing but remove restrictions. In the case of REXML, it is a large and complex enough system that it's easy to break without some type or interface checks. However, it's also flexible from that. The question is -- is it useful to have REXML::Node-alike classes, or should it be type checked? I think I'd have been bitten less if types were checked, but I may get annoyed when I go to use REXML and find I can't just pass in something similar and have it work. We'll see. A mixin might be a suitable substitute -- it's possible to do interface checking with mixins. Amrita does it (with Amrita::ExpandByMember), and I find myself wishing that ActiveRecord did the same. I find myself checking for Enumerable at times, because it's a good interface definition. Duck typing isn't perfect everywhere. With highly type-dependent and complex systems like REXML, I think it's an ill fit there. However, I'm glad that in general, Ruby is so dynamically typed -- I do some strange things that wouldn't work in other languages, and the ability for things like dRb to be so lightweight is a testament to the philosophy.