On 9/20/06, Ola Bini <ola.bini / ki.se> wrote:
> Austin Ziegler wrote:
> > On 9/19/06, Ola Bini <ola.bini / ki.se> wrote:
> >> Actually, the name is more about the fact that you can use regular
> >> duck-type techniques (like respond_to?),
> > Note: if you're calling #respond_to? you're not doing duck typing.
> > You're doing contract validation, perhaps, but not duck typing. Duck
> > typing is just calling a method and expecting that it will be
> > implemented. It's *trusting* your callers to do the right thing.
> >
> > Now, I sometimes use #respond_to? -- but it isn't duck typing.
> >
> > Additionally, if it's an external validation suite -- I haven't looked
> > at the project -- it isn't really checking against live code.
> I beg to differ, and according to the pick-axe, this interpretation is
> correct. Duck-typing isn't to *trust* your callers. Duck typing is to
> check if something quacks like a duck and walks like a duck. Well, me
> calling respond_to? checks if the object quacks and walks. What you are
> describing isn't a technique, it is the absence of a technique.

You're incorrect here. I do not need to check if someone responds to
#<< if I want to call #<< on an object. I simply call #<<. I then
*document* that I'm calling #<< on a received object and that if you
don't implement #<< in a sensible way, your code is going to break.

I trust my callers not to be stupid and send me an object that doesn't
implement #<< in this case. Checking for #<< would be the height of
stupidity.

Duck typing is treating an object like the type of object you need it
to be. It's a technique, not an absence. It's learning to let go of
the false sense of security that static typing gives people (and it
*is* a false sense of security). In my early days with Ruby, I
implemented #method_missing on a few things but didn't implement
#respond_to? on those same things. My code would have probably worked
with my style of duck typing, but would definitely not pass
validation.

Method presence validation is really useful during testing, but is of
limited use outside of it. (However, I have used it for other things
to add methods dynamically to classes, too.)

-austin
-- 
Austin Ziegler * halostatue / gmail.com * http://www.halostatue.ca/
               * austin / halostatue.ca * http://www.halostatue.ca/feed/
               * austin / zieglers.ca