On Tue, Jul 25, 2006 at 01:05:11PM +0900, Dumaiu wrote:
runtime errors. For Ruby, where every error is runtime, duck typing
is the raw form of its behavior, and should be recognized as quickly as
possible, because it isn’t always good. At its utmost, duck typing is
the total absence of validation. You are saying that nothing about
your client’s program is worth trying to predict, that ‘if it works, it
works,’ and if it doesn’t, he’ll know because it just blew up.
No. That’s not duck typing, that is shoddy programming.
You may have a rescue clause right after your doubtful code,
or somewhere else upwards in your call stack. Or it might be
guaranteed by application semantics (as opposed to static typeing)
that your object can handle the messages sent to it.
My take on duck typing:
- allows sending messages to objects without prior validations.
- breaks up dependencies on a strict class hierarchy by allowing to send
any message to any object.
Neither of them requires that a program may blow up.
Hmm. I see where type-checking came from. As far as Ruby best
practices go, I think ‘duck typing’ just means using respond_to?() more
than kind_of?() and include?(). If you really wanted to, dumping class
use entirely and turning Ruby into a prototype-based language wouldn’t
be too hard: you’d use Object::new() only, define only singleton
methods, and extend objects with clone(); and if you wanted to
delegate, you’d keep an array of references of “parent” objects,
Perl-style.
Sometimes it might be better to use #responds_to? early, and many
people will still consider this duck typing. It may be less “pure”
than handling NoMethodError, but it might be just the right tool
depending on your situation.
Even #kind_of? can have its merit in certain situations. I wouldn’t
call code using it to rely on duck typing though, but that’s not a bad
thing per se.
Prototype based languages are tangential to this, and need not be
drawn into this discussion here.
Personally, I feel that Ruby needs more, rather than less, type
safety, to balance its natural inclination otherwise and because no
amount of ‘duck typing’ disaffirms that erroneous behavior is best
caught as soon as possible. But I don’t think anyone would advocate a
return to rigid inheritance checking, which realization the deprecation
of type() notably indicates.
By your argument, exceptions are bad too, because they catch errors
“late”. And IMHO exception handling was one of the better points when
going from C to C++. Think of duck typing as yet another technique not
to litter your code with error checking, but still do it
somewhere/sometime. You do handle C++ exceptions too, do you, if not
neccessarily right next to throwing them?
I repeat: duck typing is not to produce bombing out code, but a new*
way to handle dynamic and flexible type validation “late”, with the
objective to ease development and produce clean code.
- actually not. But many people coming from C++ or Java just don’t know
prior OOP languages.
-Jürgen