Newsgroups: comp.object

In article ... Bob Hathaway <objsys@netcom.com> writes:

   [re my comment ``no'' to his rhetorical question about ``aren't methods
   enough?'']

[by the way, your messages have got woder; any change of coming back down a few
characters in linewidth? I've reformatted a little.]

   I think you're wrong again, Chris; it was really a rhetorical question, at
   least in the general function case (I was wondering about CLOS 
   idiosyncrasies that I'm not aware of).  I'm not a CLOS programmer but it 
   looks to me like requiring the "virtual" keyword in C++; its not necessary
   and some have  argued can interfere with reuse - what if a "virtual" is 
   forgotten in an unchangeable library?  

Terminological problems, perhaps.

   I was hoping a real CLOS programmer could provide some insight since CLOS 
   isn't my specialty but is quite interesting (and discussed).
   It looks to me like generic functions were just added along with methods.
   Anyway just to be more clear, I was obviously referring to CLOS generic
   functions and methods and not to CL generic functions.

CL has what one might call ``ordinary'' functions -- that is, the call of a
function ``fred'' plonks you in a piece of code which was attatched to the name
``fred'', typically by a ``defun'' construction. No clever machinery (by CL
standards!) is involved in selecting the code; one may regard the name as the
index in a table of function definitions. 

CLOS introduces ``generic functions'' and ``methods''. A generic function is a
function (that is, all the normal CL function operations apply -- pass as
parameter, resturn as result, store in generalised variables, apply)), but it's
body is *not* given all in one place. Instead a GF's implementation is given
piece-by-piece. Each piece is a *method*, and a method definition gives the
name of the generic function of which it is a part, and the constraints on the
types of the arguments for which this method can be applied.

When a generic function is called, it looks at the (types of) all (in
principle) its arguments, finds its applicable methods, and calls the ``right
one'' (loosely, the most specific). (There's all sorts of other details around,
but that's the general picture.)

Now, CLOS can suffer from the same problem as C++ when a function is not made
generic -- you can't [*1] then incrementally extend it, just as you can't
retrospectively make a C++ method virtual. But if a CLOS programmer is going to
expect inheritance *at all*, then they will use generic functions; whereas in
C++ the programmer *can* use ``inheritance'' without VFs. So that part of the
argument is, I think, not practically relevant.

   The (apparently) extra construct really surprises me since CLOS doesn't 
   usually require extra constructs.  

CLOS sees there being two activities, that of defining/declaring generic
functions (which are about interfaces) and that of defining/declaring methods
for implementing that functionality *for particular argument combinations*.

Hence my remark that methods, on their own, were NOT enough; you need [*1] the
generic functions to invoke them.

   For example, CLOS doesn't require forward declarations of classes and I 
   believe even allows for their general use (at least for parents).

Lisp in general isn't too keen on requiring forward declarations. I *think* the
compiler needs to know the actual class definition if you want to get at
structure slots directly, but that may depend on your definition of
``directly''. Lots of things get done by calling functions, so, as long as you
have a name to apply, the system can fill in the code later.

[*1] Actually, CL has enough machinery that a Lisp programmer *can*
retrospectively extend a function, provided it hasn't been coded in-line. It's
something like this:

* save the old function away
* replace it with a generic function
* define a method for that GF which calls the saved function
	(specialise it appropriately)
* continue to define methods on this GF

Of course, one would rather not do that, and things could get tricky if several
people *all* tried to do it. Which is why GFs would get used in the first
place. 
--

Regards, Kers.              | ``Remember Thor Five!'' (Earthman, Come Home)
Newsgroups: comp.object