Re: Closures versus objects

James Y Knight <[email protected]>
Newsgroups gmane.comp.lang.lightweight
Message-ID <[email protected]>
On Feb 20, 2007, at 2:49 AM, Pascal Costanza wrote:
> On 20 Feb 2007, at 04:58, Dave Roberts wrote:
>> Which brings up a good point. Given this observation, why wasn't  
>> LENGTH
>> made a GF when CL was specified? Was CLOS such a late addition and
>> everybody was afraid of the possible performance impact? Surely,  
>> there
>> was not technical reason that it couldn't have been made generic,  
>> as you
>> say; all old code would have continued working, no?
>>
>> Over the past couple years, I have really learned to appreciate  
>> CLOS and
>> this is one of those examples where it seems like a good idea wasn't
>> taken far enough.
>
> Here is a quote from http://groups.google.com/group/comp.lang.lisp/ 
> msg/3cad948789e116e9
>
> "The reason many operators are not generic [in Common Lisp] is that  
> deciding how to do
> them generically is tricky and people disagree on the definition. I
> recall the NIL project (JonL White, Jonathan Rees, and Rick Bryan) ran
> afoul of a generic definition for LENGTH enough that let us worry.  
> Consider:
>  (defmethod length ((x cons)) (+ 1 (length (cdr x))))
>  (defmethod length ((x null)) 0)
>  (defmethod length ((x string)) (array-dimension x 0))
> Now think about
>  (length '(a b . "foo"))
> The point is that often independent definitions invite what I'll  
> call the
> "modularity problem" where definitions look ok in isolation but  
> surprising
> effects happen in combination.  Similar to what
>  (+ 1 2 "foo")
> might do if we allowed + to be generic."

This seems like a bit of a strange argument. Consider that length, as  
currently defined, does indeeed work on lists and strings. An obvious  
implementation not using generic functions would be:

(defun length (x)
  (typecase x
   (null 0)
   (cons (+ 1 (length (cdr x))))
   (string (array-dimension x 0))))

which, of course, has the exact same behavior as the generic length  
above. That choice of behavior is simply a choice in specification,  
and really has very little to do with whether you use a generic  
function or not.

James
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.