Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Namespaces of class slot names and accessors

5 views
Skip to first unread message

Paolo Amoroso

unread,
Apr 25, 2004, 7:35:47 AM4/25/04
to
To which namespace do class slot names belong? And slot accessors?

More specifically, I would like to know whether it is possible for
different classes--in the same package--to have slots with the same
names, such as:

(defclass class1 ()
((same-slot :initarg :same-slot)))

(defclass class2 ()
((same-slot :initarg :same-slot)))

or for different classes to have slots with the same accessor names:

(defclass class1 ()
((class1-slot :initarg :class1-slot :accessor same-accessor)))

(defclass class2 ()
((class2-slot :initarg :class2-slot :accessor same-accessor)))


Paolo
--
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film

Scott McKay

unread,
Apr 25, 2004, 8:29:11 AM4/25/04
to

"Paolo Amoroso" <amo...@mclink.it> wrote in message
news:878ygk9...@plato.moon.paoloamoroso.it...

> To which namespace do class slot names belong? And slot accessors?
>
> More specifically, I would like to know whether it is possible for
> different classes--in the same package--to have slots with the same
> names, such as:
>
> (defclass class1 ()
> ((same-slot :initarg :same-slot)))
>
> (defclass class2 ()
> ((same-slot :initarg :same-slot)))
>
> or for different classes to have slots with the same accessor names:
>
> (defclass class1 ()
> ((class1-slot :initarg :class1-slot :accessor same-accessor)))
>
> (defclass class2 ()
> ((class2-slot :initarg :class2-slot :accessor same-accessor)))
>

'same-accessor' is just the name of a generic function for
the slot's accessor method, so this will work. However,
even though there is no enforcement, I think that you
should be sure that 'same-accessor' implements the
same sort of semantics for each class.

FWIW, when I share accessor names between multiple
classes, I first define generic functions for those accessors.

As for slot names, well, they're just names of symbols.
After the Dylan experience, slot names as distinct from
accessors seem like a mistake to me now.


Rainer Joswig

unread,
Apr 25, 2004, 10:43:25 AM4/25/04
to
In article <raOic.30059$IW1.1341220@attbi_s52>,
"Scott McKay" <scott...@comcast.net> wrote:

One thing one could do is to use an uninterned symbol
as the slot name, so you would be forced to
use an accessor to read/write the slot
(unless you have some introspective capability
to get the class slot-names).

(defclass foo () ((#:bar :accessor foo-bar :initform 0)))

Now (slot-value (make-instance 'foo) '#:bar) does not work,
since #:bar and another #:bar are not eql.

Kalle Olavi Niemitalo

unread,
Apr 25, 2004, 12:55:33 PM4/25/04
to
Paolo Amoroso <amo...@mclink.it> writes:

> More specifically, I would like to know whether it is possible for
> different classes--in the same package--to have slots with the same
> names, such as:
>
> (defclass class1 ()
> ((same-slot :initarg :same-slot)))
>
> (defclass class2 ()
> ((same-slot :initarg :same-slot)))

It is possible. However, if you define a CLASS3 that has both
CLASS1 and CLASS2 as its superclasses, then each instance of
CLASS3 will have only one slot named SAME-SLOT, which may cause
conflicts. If you know such a class won't be needed, there's no
problem.

Tim Bradshaw

unread,
Apr 26, 2004, 8:36:51 AM4/26/04
to
Rainer Joswig <jos...@lispmachine.de> wrote in message news:<joswig-33EADC....@news.fu-berlin.de>...

>
> One thing one could do is to use an uninterned symbol
> as the slot name, so you would be forced to
> use an accessor to read/write the slot
> (unless you have some introspective capability
> to get the class slot-names).
>
> (defclass foo () ((#:bar :accessor foo-bar :initform 0)))
>
> Now (slot-value (make-instance 'foo) '#:bar) does not work,
> since #:bar and another #:bar are not eql.

I have a wrapper around DEFCLASS that lets you define various `magic'
kinds of slots - in particular slots which are private to a class -
like this. You need slightly more than just using a gensym, because
using a gensym doesn't let you redefine the class easily (well, you
can redefine it, but it doesn't know that the slots are the same).
I've never actually used the wrapper though, so for what I do at least
it didn't seem to be useful.

--tim

0 new messages