But again, that flexibility is LOST when you have to pass an
object to a method that REQUIRES it to behave a certain way.
Using “kind_of” testing /further/ reduces flexibility by requiring
particular inheritance structures in addition to the protocol requirements.
No, the flexibility is gone already; kind_of? just provides a more informative
way to find out what went wrong.
Having absolutely NO mechanism leaves people who need to check
for certain methods and parameters hanging in the wind.
Can you give an example of where someone would NEED to check.
Okay, you got it. Here’s an example of how this sort of type checking would
be useful:
I am the developer of a web server library. I have written a library which
contains a class that is used as the server-end of an HTTP request. It is
essentially a socket that I use to read and write.
Now, Bob the application developer decides to use my library to write himself
a little HTTP applet. He writes a CGI method that hooks into my library and
when he runs the applet, he can pass form data to the server and his little
CGI hook does something fun.
But Bob’s CGI method is not working as intended, so he decides to do some
debugging.
He decides to build the full HTTP request as a text file, and pipe the file to
the CGI method through my library by opening it with the File object and
passing a File object where I was expecting a Socket object.
The code goes BOOM. The library is trying to make calls to methods that don’t
exist. Bob sees what methods are being requested, but he has no reference to
them. Nowhere does he see “I expected a Socket.” It’s just not there. The
calls are being made, but what are they? What interface does my library
want? He has no clue. All he sees are error messages, but since he is not
me, he doesn’t know I expected a Socket object. It’s never declared
anywhere.
Now, let’s add in interface checking.
My methods all require that objects being passed in declare themselves with
the Socket interface signature.
Now replay the scenario.
Bob gets a message “that object does not implement the Socket interface.”
Bob knows what went wrong. He looks up the Socket interface, modifies his
File object to fake the interface and provide my library with all the
read/write functions it expects, with a full reference to what parameters are
expected and so on.
This is not a complicated scenario, either. I would wager that some people
monitoring this newsgroup have actually tried to do this at some point in
their programming career, perhaps even tried it with Ruby.
Getting an error message that an object is of the type required, is VERY
useful in some places. Not always, but in some places.
Sean O'Dell
···
On Wednesday 19 November 2003 11:47 am, Weirich, James wrote: