From: ·········@gmail.com
Subject: DEFCLASS vs. DEFSTRUCT?
Date: 
Message-ID: <1157409794.819543.169110@m79g2000cwm.googlegroups.com>
Hi, all,

I'm in the process of writing my first quasi-serious program in Lisp.
In it, I'm using quite a few classes and methods, which aren't things
I've worked much with before, but which strike me as particularly
well-suited to the problem I'm trying to solve (which is a Monte
Carlo-based simulation). I've just been sort of using DEFCLASS classes
so far, but I'm aware that I could, if I wanted, use DEFSTRUCT structs
instead, and the methods would still be able to specialize on them and
such.

What's the advantage of using one or the other?

Thanks in advance,
Matt

From: Barry Margolin
Subject: Re: DEFCLASS vs. DEFSTRUCT?
Date: 
Message-ID: <barmar-7C630E.20281004092006@comcast.dca.giganews.com>
In article <························@m79g2000cwm.googlegroups.com>,
 ·········@gmail.com wrote:

> Hi, all,
> 
> I'm in the process of writing my first quasi-serious program in Lisp.
> In it, I'm using quite a few classes and methods, which aren't things
> I've worked much with before, but which strike me as particularly
> well-suited to the problem I'm trying to solve (which is a Monte
> Carlo-based simulation). I've just been sort of using DEFCLASS classes
> so far, but I'm aware that I could, if I wanted, use DEFSTRUCT structs
> instead, and the methods would still be able to specialize on them and
> such.
> 
> What's the advantage of using one or the other?

DEFCLASS supports multiple inheritance, and provides more control over 
how slots are accessed.  You can also make use of WITH-SLOTS and 
WITH-ACCESSORS macros with classes, but not structures.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Pascal Costanza
Subject: Re: DEFCLASS vs. DEFSTRUCT?
Date: 
Message-ID: <4m4jbbF4f7jqU1@individual.net>
Barry Margolin wrote:
> In article <························@m79g2000cwm.googlegroups.com>,
>  ·········@gmail.com wrote:
> 
>> Hi, all,
>>
>> I'm in the process of writing my first quasi-serious program in Lisp.
>> In it, I'm using quite a few classes and methods, which aren't things
>> I've worked much with before, but which strike me as particularly
>> well-suited to the problem I'm trying to solve (which is a Monte
>> Carlo-based simulation). I've just been sort of using DEFCLASS classes
>> so far, but I'm aware that I could, if I wanted, use DEFSTRUCT structs
>> instead, and the methods would still be able to specialize on them and
>> such.
>>
>> What's the advantage of using one or the other?
> 
> DEFCLASS supports multiple inheritance, and provides more control over 
> how slots are accessed.  You can also make use of WITH-SLOTS and 
> WITH-ACCESSORS macros with classes, but not structures.

It's also the case that classes are easier to work with during 
development because it's specified that you can change their definition 
as you go, which updates existing instances and thus considerably 
reduces the need to start from scratch when you have made changes. There 
are also cases where this ability to change classes on the fly can be 
useful in deployed programs.

Since structs support only single inheritance, provide less control over 
slot accesses and are generally less flexible - at least as specified -, 
they are typically more efficiently implementable. Therefore, a rule of 
thumb is to use classes by default for convenience, and only use structs 
when you need to squeeze out some efficiency.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/