From: Thomas A. Russ
Subject: Re: best way of assigning slot values from parent class instances to child class instances
Date: 
Message-ID: <ymiejz1pwpm.fsf@sevak.isi.edu>
"Stormcoder" <·········@gmail.com> writes:

> I have two classes one subclasses the other. What is the best way of
> assigning slot values from a parent class instance to a child class
> instance. Right now I am just assigning each slot with setf. Is there a
> better way of doing that?

I don't quite understand what you are trying to do.
Could you perhaps give an example?


-- 
Thomas A. Russ,  USC/Information Sciences Institute

From: Stormcoder
Subject: Re: best way of assigning slot values from parent class instances to child class instances
Date: 
Message-ID: <1147466327.488148.207000@i39g2000cwa.googlegroups.com>
What I am trying to do is understand the lispy idioms for doing certain
things. In this case I'm trying to determine if there is a more lispy
way of assigning field values from one object to another, other than
member wise assignment. In C++, the default for two objects of the same
class when one is assigned to another is a memberwise copy (which is
bad for pointer members). You can create copy contructors and overload
the assignment operator to get the right behavior.
(The above is just example so no comments on the viability of C++)

ie. I've got an Auto class with some basic automobile functionality. I
have a Pickup sub-class of Auto. I've been doing some operations on an
Auto object and I want to use the Auto object to initialize a Pickup
object. (Might not be an initialization but could be an assignment or
something) The obvious way is just to have a method that does a member
wise assignment of fields.

The question is, is that the idiomatic way of doing this or is there a
better way?

Another question that occured to me is, Is there the equivalent of a
cast, in this case from pickup to auto and back?
 C++ equivalent of:

hAuto &auto = dynamic_cast<Auto&>(pickup);
From: Bill Atkins
Subject: Re: best way of assigning slot values from parent class instances to child class instances
Date: 
Message-ID: <874pzvaqop.fsf@rpi.edu>
"Stormcoder" <·········@gmail.com> writes:

> What I am trying to do is understand the lispy idioms for doing certain
> things. In this case I'm trying to determine if there is a more lispy
> way of assigning field values from one object to another, other than
> member wise assignment. In C++, the default for two objects of the same
> class when one is assigned to another is a memberwise copy (which is
> bad for pointer members). You can create copy contructors and overload
> the assignment operator to get the right behavior.
> (The above is just example so no comments on the viability of C++)
>
> ie. I've got an Auto class with some basic automobile functionality. I
> have a Pickup sub-class of Auto. I've been doing some operations on an
> Auto object and I want to use the Auto object to initialize a Pickup
> object. (Might not be an initialization but could be an assignment or
> something) The obvious way is just to have a method that does a member
> wise assignment of fields.
>
> The question is, is that the idiomatic way of doing this or is there a
> better way?

(let ((my-pickup (make-instance 'auto :make 'ford :model 'f150)))
  (change-class my-pickup (find-class 'pickup)))

assuming PICKUP is a subclass of AUTO.

>
> Another question that occured to me is, Is there the equivalent of a
> cast, in this case from pickup to auto and back?
>  C++ equivalent of:
>
> hAuto &auto = dynamic_cast<Auto&>(pickup);
>

CHANGE-CLASS?
From: Stormcoder
Subject: Re: best way of assigning slot values from parent class instances to child class instances
Date: 
Message-ID: <1147578718.107725.74580@d71g2000cwd.googlegroups.com>
This is what I was looking for.
From: Tayssir John Gabbour
Subject: Re: best way of assigning slot values from parent class instances to child class instances
Date: 
Message-ID: <1147525533.425261.213410@j33g2000cwa.googlegroups.com>
Stormcoder wrote:
> ie. I've got an Auto class with some basic automobile functionality. I
> have a Pickup sub-class of Auto. I've been doing some operations on an
> Auto object and I want to use the Auto object to initialize a Pickup
> object. (Might not be an initialization but could be an assignment or
> something) The obvious way is just to have a method that does a member
> wise assignment of fields.
>
> The question is, is that the idiomatic way of doing this or is there a
> better way?

Are you looking for something like the following?

;; requires metaobject protocol
(defmethod copy-slots (from to)
  "For the slots the two objects have in common, copy slot values from
FROM to TO.

Returns TO."
  (let* ((from-class (class-of from))
         (to-class   (class-of to))
         (from-slots (mapcar #'slot-definition-name
                             (class-slots from-class)))
         (to-slots   (mapcar #'slot-definition-name
                             (class-slots to-class))))
    (loop for name in (intersection from-slots to-slots)
          do (setf (slot-value to name)
                   (slot-value from name)))
    to))

(defun make-prototype (super subclassname)
  "Makes an instance of class named by SUBCLASSNAME, and initializes it
with SUPER's slot values."
  (let ((new-instance (make-instance subclassname)))
    (copy-slots super new-instance)))


Tayssir
From: Tayssir John Gabbour
Subject: Re: best way of assigning slot values from parent class instances to child class instances
Date: 
Message-ID: <1147544773.905718.69180@v46g2000cwv.googlegroups.com>
Tayssir John Gabbour wrote:
> Stormcoder wrote:
> > ie. I've got an Auto class with some basic automobile functionality. I
> > have a Pickup sub-class of Auto. I've been doing some operations on an
> > Auto object and I want to use the Auto object to initialize a Pickup
> > object. (Might not be an initialization but could be an assignment or
> > something) The obvious way is just to have a method that does a member
> > wise assignment of fields.
> >
> > The question is, is that the idiomatic way of doing this or is there a
> > better way?
>
> Are you looking for something like the following?

Hmm, making that code less verbose and specific...

(defmethod slot-names (object)
  (mapcar #'slot-definition-name (class-slots (class-of object))))

(defmethod copy-slots (from to)
  (loop for name in (intersection (slot-names from) (slot-names to))
        do (setf (slot-value to name) (slot-value from name))
        finally (return to)))

(defmethod make-prototype (object new-classname)
  (copy-slots object (make-instance new-classname)))


Tayssir