From: Joe Marshall
Subject: Re: defgeneric, defstruct and no CLOS
Date:
Message-ID: <hdavt59o.fsf@alum.mit.edu>
verec <·····@mac.com> writes:
> I'm again a bit confused. Basically, what I want to do, is to be
> able to define a single "function" name, reused (ie: "redefined")
> for various combinations of arguments such that I can:
>
> (draw conclusion)
> (draw bank-account)
> (draw shape)
>
> I kind of sense that in CL parlance this all what a "generic function"
> is about.
Yes and no. The word `draw' in the above examples has very different
meanings (that is, if you don't want (draw bank-account) to make a
little box with some money in it on the screen). CLOS is specifically
*not* designed with this in mind.
On the other hand, if your example was this:
(draw shape x y)
(draw text x y)
(draw cursor x y)
Then CLOS *is* what you want.
> Where I start to get confused is when the CLHS starts talking about
> ``objects'' according to which the appropriate method of the generic
> function is selected.
>
> I guess my problem is also a terminlogy problem. As I understand
> it, most everything in CL is an object (in the sense: can be garbage
> collected) whether it is a "cons cell" or an instance of a CLOS class.
> Does that mean that I can specialize a method on a cons cell type?
Yes. CLOS integrates with the rest of the common lisp type system.
> My real goal, at this stage, is to start defining my data structures using
> defstruct, and be able to define "context free" names for functions, which,
> when "specialized" for a given "type" (as far as defstruct defines types)
> will resolve to the correct body of code.
While you *can* use defstructs, classes are supposed to be a
replacement. It is perfectly reasonable to use a class as sort of a
verbose version of defstruct and make no use of inheritance or other
fancy object features. In fact, I'd recommend it.
> Do I need to understand the whole of CLOS first, or is there a "shortcut"
> that would allow me to experiment quicker?
Dive in. CLOS is big, and 90% of it is magic that is not necessary to
know (but *really* nice to have in the background). You can start
with the simplest aspects of CLOS and gain a great amount of benefit.
> 20 years or so ago, when "Inside Mac" in 3 volumes first appeared,
> the joke was that: "In order to understand Inside Mac, you have first
> to ... understand Inside Mac!" -- and I must admit that I had to read
> slowly the three volumes, from cover to cover, at least three times,
> before I got to the "Aha!".
Nah, I tried that with CLOS and decided it was `too hairy'. Then I
started working with CLOS on a project and saw the light.
> Is it the same with CLOS/defgeneric/defmethod, or is there a simple(r)
> way to basically perform function overloading?
A terminology point. Overloading is bad. Overloading means taking a
name and using it for completely different purposes and expecting to
disambiguate by context. A generic function is a function that
applies to a wide range of argument types and does `the same sort of
thing' to each object.
> I get even more confused when I start to consider that in CL, values
> are typed, but identifiers (ie symbols) are not, unless ``(declare
> (sometype x))''
> and I don't see (yet?) how ``overloading'' would work nor how I would
> go about to
> (defun draw (conclusion) ...)
> (defun draw (shape) ...)
> and have the right code be called when a form simply contains
> (draw x)
Right. That's why overloading doesn't work.
> One possible answer is that I'm too "OO biased" and can't see the world
> otherwise than "encapsulated state" on one hand, and "state modifying
> functions" on the other hand... Yet, I'm not too convinced by Scheme-
> like functional programming either. Is there a third way? I'd love to get
> a glimpse at it :-)
Yes. Forget `encapsulation'. CLOS gives you functions on steroids.
Joe Marshall wrote:
> verec <·····@mac.com> writes:
>>20 years or so ago, when "Inside Mac" in 3 volumes first appeared,
>>the joke was that: "In order to understand Inside Mac, you have first
>>to ... understand Inside Mac!" -- and I must admit that I had to read
>>slowly the three volumes, from cover to cover, at least three times,
>>before I got to the "Aha!".
>
>
> Nah, I tried that with CLOS and decided it was `too hairy'.
You mean Keene? The menu at TGIF's is bigger! defclass, defmethod,
:before, :after, :around -- where is the hard part?!
You must have started with AMOP by mistake.
--
Kenny
Why Lisp? http://wiki.alu.org/RtL_Highlight_Film
"I've wrestled with reality for 35 years, Doctor, and I'm happy to state
I finally won out over it."
Elwood P. Dowd, "Harvey", 1950
From: Joe Marshall
Subject: Re: defgeneric, defstruct and no CLOS
Date:
Message-ID: <8xw6trio.fsf@alum.mit.edu>
Kenny Tilton <·······@nyc.rr.com> writes:
> Joe Marshall wrote:
>> verec <·····@mac.com> writes:
>>>20 years or so ago, when "Inside Mac" in 3 volumes first appeared,
>>>the joke was that: "In order to understand Inside Mac, you have first
>>>to ... understand Inside Mac!" -- and I must admit that I had to read
>>>slowly the three volumes, from cover to cover, at least three times,
>>>before I got to the "Aha!".
>> Nah, I tried that with CLOS and decided it was `too hairy'.
>
> You mean Keene? The menu at TGIF's is bigger! defclass, defmethod,
> :before, :after, :around -- where is the hard part?!
>
> You must have started with AMOP by mistake.
What can I say? I'm easily confused. But Sonya hadn't written her
book when I first encountered Common LOOPS --- er, I mean CLOS --- and
LMI was pushing Object Lisp at the time.