From: Chaitanya
Subject: Importing slot values from an existing object
Date: 
Message-ID: <1154951016.688195.126830@i3g2000cwc.googlegroups.com>
Hi,

I have a query about CLOS. I've got a class A, and a subclass of A
called B, with some additional slots. The problem is, if I have an
instance of A, how do I create an instance of B so that it imports all
the slots from that instance of A? Of course I can write a function
that does this, but it will only be specific to A and B. Is there any
general way to do this with any class and subclass?

Thanks,
Chaitanya

From: Dr. John A.R. Williams
Subject: Re: Importing slot values from an existing object
Date: 
Message-ID: <877j1krcrz.fsf@mailhub.aston.ac.uk>
See update-instance-for-different-class and
update-instance-for-redefined-class

>>>>> "Chaitanya" == Chaitanya  <··@chaitanyagupta.com> writes:

    Chaitanya> Hi, I have a query about CLOS. I've got a class A, and
    Chaitanya> a subclass of A called B, with some additional
    Chaitanya> slots. The problem is, if I have an instance of A, how
    Chaitanya> do I create an instance of B so that it imports all the
    Chaitanya> slots from that instance of A? Of course I can write a
    Chaitanya> function that does this, but it will only be specific
    Chaitanya> to A and B. Is there any general way to do this with
    Chaitanya> any class and subclass?

    Chaitanya> Thanks, Chaitanya


-- 
Dr. John A.R. Williams 
Electronic Engineering, Aston University, Birmingham B4 7ET
Tel: 0121 359 3621 x 4989      Fax: 0121 359 0156
www: http://www.ee.aston.ac.uk/staff/willijar 
PGP key: 6606795A185C384C
From: Pierre THIERRY
Subject: Re: Importing slot values from an existing object
Date: 
Message-ID: <pan.2006.08.07.11.48.53.185520@levallois.eu.org>
Le Mon, 07 Aug 2006 04:43:36 -0700, Chaitanya a écrit :
> Is there any general way to do this with any class and subclass?

If *a* is such an instance of A, just do:

(change-class *a* 'B)

Doubtfully,
Nowhere man
-- 
···········@levallois.eu.org
OpenPGP 0xD9D50D8A
From: Chaitanya
Subject: Re: Importing slot values from an existing object
Date: 
Message-ID: <1154955245.352239.294690@75g2000cwc.googlegroups.com>
Pierre THIERRY wrote:
> Le Mon, 07 Aug 2006 04:43:36 -0700, Chaitanya a écrit :
> > Is there any general way to do this with any class and subclass?
>
> If *a* is such an instance of A, just do:
>
> (change-class *a* 'B)

Yes, that works. But the only problem is, it modifies *a*. I want to
keep the original object too. So how do I copy an object? I can't find
a way to do it (limited CLOS knowledge here).
Looking at the definitions of change-class and
update-instance-for-different-class, I tried writing an :after method
which I had hoped would return the original object, but it didn't work
-

(defmethod update-instance-for-different-class :after
    ((previous A) (current A) &rest initargs)
  previous)

What did I miss?
Chaitanya
From: Ken Tilton
Subject: Re: Importing slot values from an existing object
Date: 
Message-ID: <78LBg.2923$3U4.187@fe12.lga>
Chaitanya wrote:
> Pierre THIERRY wrote:
> 
>>Le Mon, 07 Aug 2006 04:43:36 -0700, Chaitanya a �crit :
>>
>>>Is there any general way to do this with any class and subclass?
>>
>>If *a* is such an instance of A, just do:
>>
>>(change-class *a* 'B)
> 
> 
> Yes, that works. But the only problem is, it modifies *a*. I want to
> keep the original object too. So how do I copy an object? I can't find
> a way to do it (limited CLOS knowledge here).

Iterate over the slots using variable methods (since CLOS is not 
standardized) to determine what slots any given class has and so which 
you have to copy. The method might be class-slots, or you might have to 
take the union of class-direct-slots and class-indirect-slots. (apropos 
"SLOTS") might help.

Basically you do have to dig into your Lisp's doc and learn about CLOS, 
it is pretty big.

kt (wondering why you want to copy an instance)


-- 
Cells: http://common-lisp.net/project/cells/

"I'll say I'm losing my grip, and it feels terrific."
    -- Smiling husband to scowling wife, New Yorker cartoon
From: Chaitanya
Subject: Re: Importing slot values from an existing object
Date: 
Message-ID: <1155027233.264795.17480@75g2000cwc.googlegroups.com>
Ken Tilton wrote:

> Iterate over the slots using variable methods (since CLOS is not
> standardized) to determine what slots any given class has and so which
> you have to copy. The method might be class-slots, or you might have to
> take the union of class-direct-slots and class-indirect-slots. (apropos
> "SLOTS") might help.
>
> Basically you do have to dig into your Lisp's doc and learn about CLOS,
> it is pretty big.

Thanks. Seems like MOP is the way to go. And I guess deep-copy,
shallow-copy will also be implemented this way. Let me just dive into
CLOS...

>
> kt (wondering why you want to copy an instance)

Don't really need to copy the instance. I want two different instances
- one for the class and the other for its subclass.

Chaitanya
From: Ken Tilton
Subject: Re: Importing slot values from an existing object
Date: 
Message-ID: <sqZBg.1025$Fs3.1014@fe09.lga>
Chaitanya wrote:

> Don't really need to copy the instance. I want two different instances
> - one for the class and the other for its subclass.

Sounds even fishier. What would the names of the two classes be?

kt

-- 
Cells: http://common-lisp.net/project/cells/

"I'll say I'm losing my grip, and it feels terrific."
    -- Smiling husband to scowling wife, New Yorker cartoon
From: Chaitanya
Subject: Re: Importing slot values from an existing object
Date: 
Message-ID: <1155040825.796721.236230@m73g2000cwd.googlegroups.com>
Ken Tilton wrote:
> Chaitanya wrote:
>
> > Don't really need to copy the instance. I want two different instances
> > - one for the class and the other for its subclass.
>
> Sounds even fishier. What would the names of the two classes be?

There's more than two classes. I have one base class, and a few
subclasses with specialized methods for them. I want to preserve the
original object so I can export its slots to instances of different
subclasses.

Chaitanya

>
> kt
>
> --
> Cells: http://common-lisp.net/project/cells/
>
> "I'll say I'm losing my grip, and it feels terrific."
>     -- Smiling husband to scowling wife, New Yorker cartoon
From: Rob Warnock
Subject: Re: Importing slot values from an existing object
Date: 
Message-ID: <i_6dndjJIszvKUTZnZ2dnUVZ_rWdnZ2d@speakeasy.net>
Chaitanya <········@gmail.com> wrote:
+---------------
| Ken Tilton wrote:
| > Sounds even fishier. What would the names of the two classes be?
| 
| There's more than two classes. I have one base class, and a few
| subclasses with specialized methods for them. I want to preserve the
| original object so I can export its slots to instances of different
| subclasses.
+---------------

Is there only one such "original object"? Are the values of its
slots essentially read-only? If so, have you looked into using
the (:ALLOCATION :CLASS) slot option when defining A? Then all
instances of A, *including* all instances of B, will share the
same slots. [Well, for those slots of A for which you specify
(:ALLOCATION :CLASS)...]

If not, then maybe what you really want is not CLOS but a
prototype-based object system such as provided by, say, KR
(available as a part of the Garnet distribution or now by
itself, see <http://www.cliki.net/KR>).


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: ········@gmail.com
Subject: Re: Importing slot values from an existing object
Date: 
Message-ID: <1154988151.541421.263120@n13g2000cwa.googlegroups.com>
Chaitanya wrote:
> Pierre THIERRY wrote:
> > Le Mon, 07 Aug 2006 04:43:36 -0700, Chaitanya a écrit :
> > > Is there any general way to do this with any class and subclass?
> >
> > If *a* is such an instance of A, just do:
> >
> > (change-class *a* 'B)
>
> Yes, that works. But the only problem is, it modifies *a*.

what about

(defclass a ()
    ((a-slot :initarg :a-slot :accessor a-slot-of)
     ...))

(defclass b ()
    ((shared-a :initarg :a :accessor shared-a-of)
     ...<b-specific-slots>...))

(defmethod a-slot-of ((xx b))
    (a-slot-of (shared-a-of xx)))

you could black box this with a couple macros

Nick

(sorry for not typing more, I'm at a computer with an "ergonomic"
keyboard and track ball and no mouse and I've had about as much as I
can take)
From: Chaitanya
Subject: Re: Importing slot values from an existing object
Date: 
Message-ID: <1155028886.284802.116160@m79g2000cwm.googlegroups.com>
········@gmail.com wrote:
> what about
>
> (defclass a ()
>     ((a-slot :initarg :a-slot :accessor a-slot-of)
>      ...))
>
> (defclass b ()
>     ((shared-a :initarg :a :accessor shared-a-of)
>      ...<b-specific-slots>...))
>
> (defmethod a-slot-of ((xx b))
>     (a-slot-of (shared-a-of xx)))
>
> you could black box this with a couple macros

This also seems good. Thanks.
>
> Nick
>
> (sorry for not typing more, I'm at a computer with an "ergonomic"
> keyboard and track ball and no mouse and I've had about as much as I
> can take)
From: Pierre THIERRY
Subject: Re: Importing slot values from an existing object
Date: 
Message-ID: <pan.2006.08.07.14.26.34.98276@levallois.eu.org>
Le Mon, 07 Aug 2006 05:54:05 -0700, Chaitanya a écrit :
> Yes, that works. But the only problem is, it modifies *a*. I want to
> keep the original object too. So how do I copy an object?

It's up to you to define what copying your object is. In some languages,
there is a standard for this (clone method in Java, copy constructor in
C++), but not in Lisp, AFAICT.

Implement a deep-copy method for your object, then

(change-class 'B (deep-copy *a*))

Quickly,
Nowhere man
-- 
···········@levallois.eu.org
OpenPGP 0xD9D50D8A