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

Aliasing slots

4 views
Skip to first unread message

James A. Crippen

unread,
Dec 16, 2001, 10:57:59 PM12/16/01
to
Suppose I have the following classes:

(defclass pname-mixin ()
((pname :type string
:initarg :pname
:accessor pname-of)))

(defclass foo (name-mixin)
((name :type string
:initarg :name
:accessor name-of)))

Basically I want the FOO class to have a single slot NAME which is
equivalent to PNAME. Ie, NAME and PNAME should be aliases for the
same slot, such that

(let ((bar (make-instance 'foo :name "bar")))
(equal (name-of bar) (pname-of bar)))

will return T.

How do I go about aliasing a slot?

'james

--
James A. Crippen <ja...@unlambda.com> ,-./-. Anchorage, Alaska,
Lambda Unlimited: Recursion 'R' Us | |/ | USA, 61.20939N, -149.767W
Y = \f.(\x.f(xx)) (\x.f(xx)) | |\ | Earth, Sol System,
Y(F) = F(Y(F)) \_,-_/ Milky Way.

Wade Humeniuk

unread,
Dec 16, 2001, 11:11:53 PM12/16/01
to
>
> (let ((bar (make-instance 'foo :name "bar")))
> (equal (name-of bar) (pname-of bar)))

(defmethod pname-of ((obj foo))
(name-of obj))


James A. Crippen

unread,
Dec 17, 2001, 5:34:09 AM12/17/01
to
"Wade Humeniuk" <hume...@cadvision.com> writes:

That takes care of the reader, but what about the initarg?

Erik Naggum

unread,
Dec 17, 2001, 6:21:00 AM12/17/01
to
* ja...@unlambda.com (James A. Crippen)

| (defclass pname-mixin ()
| ((pname :type string
| :initarg :pname
| :accessor pname-of)))
|
| (defclass foo (name-mixin)
| ((name :type string
| :initarg :name
| :accessor name-of)))

If these are the same slot, just use the same slot name.

However, what is the point in using the mixin superclass if you repeat
the slot information in the subclass?

///
--
The past is not more important than the future, despite what your culture
has taught you. Your future observations, conclusions, and beliefs are
more important to you than those in your past ever will be. The world is
changing so fast the balance between the past and the future has shifted.

Pierre R. Mai

unread,
Dec 17, 2001, 10:19:55 AM12/17/01
to
ja...@unlambda.com (James A. Crippen) writes:

> "Wade Humeniuk" <hume...@cadvision.com> writes:
>
> > > (let ((bar (make-instance 'foo :name "bar")))
> > > (equal (name-of bar) (pname-of bar)))
> >
> > (defmethod pname-of ((obj foo))
> > (name-of obj))
>
> That takes care of the reader, but what about the initarg?

For the full complement:

(defclass pname-mixin ()
((pname :type string
:initarg :pname
:accessor pname-of)))

(defclass foo (pname-mixin)
((pname :initarg :name
:accessor name-of)))

Now instances of foo contain one slot, named pname, which has initargs
:pname and :name, readers named pname-of and name-of, and writers
named (setf pname-of) and (setf name-of).

* (let ((bar (make-instance 'foo :name "bar")))


(equal (name-of bar) (pname-of bar)))

T
* (make-instance 'foo :name "bar")

#<FOO {481BBC45}>
* (describe *)


#<FOO {481BBC45}> is an instance of class #<Standard-Class FOO {4819FAA5}>:
The following slots have :INSTANCE allocation:
PNAME "bar"
* (setf (name-of **) "foo")

"foo"
* ***

#<FOO {481BBC45}>
* (pname-of *)

"foo"


That said, I don't know what advantage you gain by having the slot in
the mixin-class...

Regs, Pierre.

--
Pierre R. Mai <pm...@acm.org> http://www.pmsf.de/pmai/
The most likely way for the world to be destroyed, most experts agree,
is by accident. That's where we come in; we're computer professionals.
We cause accidents. -- Nathaniel Borenstein

Barry Margolin

unread,
Dec 17, 2001, 10:36:07 AM12/17/01
to
In article <m3adwid...@kappa.unlambda.com>,

James A. Crippen <ja...@unlambda.com> wrote:
>"Wade Humeniuk" <hume...@cadvision.com> writes:
>
>> > (let ((bar (make-instance 'foo :name "bar")))
>> > (equal (name-of bar) (pname-of bar)))
>>
>> (defmethod pname-of ((obj foo))
>> (name-of obj))
>
>That takes care of the reader, but what about the initarg?

Define an initialize-instance or shared-initialize method that takes care
of it.

--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

Karsten Poeck

unread,
Dec 17, 2001, 4:10:48 PM12/17/01
to

Why not simply

(defclass pname&name-mixin ()
((pname :initarg :name
:initarg :pname
:accessor name-of
:accessor pname-of)))

(defclass foo (pname&name-mixin)
())

(let ((bar (make-instance 'foo :name "bar")))
(equal (name-of bar) (pname-of bar)))

t

(describe (make-instance 'foo :name "blah"))

#<FOO @ #x20fa66da> is an instance of #<STANDARD-CLASS FOO>:


The following slots have :INSTANCE allocation:

PNAME "blah"


Wade Humeniuk

unread,
Dec 17, 2001, 4:23:11 PM12/17/01
to
Thank you Karsten. Looking at the CLHS I see that that you are absolutely
right. Well, I learned my new thing for the day.

Multiple initargs and accessors and readers and writers.

Wade


James A. Crippen

unread,
Dec 17, 2001, 8:31:46 PM12/17/01
to
Erik Naggum <er...@naggum.net> writes:

> * ja...@unlambda.com (James A. Crippen)
> | (defclass pname-mixin ()
> | ((pname :type string
> | :initarg :pname
> | :accessor pname-of)))
> |
> | (defclass foo (name-mixin)
> | ((name :type string
> | :initarg :name
> | :accessor name-of)))
>
> If these are the same slot, just use the same slot name.

Not possible, see below.

> However, what is the point in using the mixin superclass if you repeat
> the slot information in the subclass?

Because the subclass originally (and there's a bunch of code that
depends on this) implemented its own NAME slot. The mixin class was
written later and although other code uses it, I'd like to see the
idiosyncratic NAME slot become combined with the PNAME slot without
having to change any of the code that uses NAME.

The solution of providing multiple initargs and accessors takes care
of the problem, I think. And this is IMHO another instance of why
it's better to use accessors than to rely on SLOT-VALUE. Thankfully
none of the existing code uses SLOT-VALUE, it all relies on accessors.

Thanks to all for the advice.

0 new messages