From: James A. Crippen
Subject: Aliasing slots
Date: 
Message-ID: <m36676birc.fsf@kappa.unlambda.com>
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 <·····@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.

From: Wade Humeniuk
Subject: Re: Aliasing slots
Date: 
Message-ID: <9vjr8f$ug5$1@news3.cadvision.com>
>
> (let ((bar (make-instance 'foo :name "bar")))
>   (equal (name-of bar) (pname-of bar)))

(defmethod pname-of ((obj foo))
    (name-of obj))
From: James A. Crippen
Subject: Re: Aliasing slots
Date: 
Message-ID: <m3adwidtjy.fsf@kappa.unlambda.com>
"Wade Humeniuk" <········@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?

'james

-- 
James A. Crippen <·····@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.
From: Barry Margolin
Subject: Re: Aliasing slots
Date: 
Message-ID: <GhoT7.7$3A2.160739@burlma1-snr2>
In article <··············@kappa.unlambda.com>,
James A. Crippen <·····@unlambda.com> wrote:
>"Wade Humeniuk" <········@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, ······@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.
From: Pierre R. Mai
Subject: Re: Aliasing slots
Date: 
Message-ID: <87r8pt50x0.fsf@orion.bln.pmsf.de>
·····@unlambda.com (James A. Crippen) writes:

> "Wade Humeniuk" <········@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 <····@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
From: Karsten Poeck
Subject: Re: Aliasing slots
Date: 
Message-ID: <9vln2n$sh0$1@news.wanadoo.es>
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"
From: Wade Humeniuk
Subject: Re: Aliasing slots
Date: 
Message-ID: <9vlnm4$fs4$1@news3.cadvision.com>
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
From: Erik Naggum
Subject: Re: Aliasing slots
Date: 
Message-ID: <3217576858921853@naggum.net>
* ·····@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.
From: James A. Crippen
Subject: Re: Aliasing slots
Date: 
Message-ID: <m3zo4hxqil.fsf@kappa.unlambda.com>
Erik Naggum <····@naggum.net> writes:

> * ·····@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.

'james

-- 
James A. Crippen <·····@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.