From: Jacktian
Subject: Can anyone show a beginner a short code as solution? CLOS
Date: 
Message-ID: <d1f47559.0210071232.2b7c848e@posting.google.com>
original code :

(defvar *members* '()
    "List of all members.")

;; define a class
(defclass family () ((age :accessor age
                          :initarg :age
                          :initform nil)
                       (name :accessor name
                               :initarg :name
                               :initform nil)))

;;; build up instances and push to hash table
(push (make-instance 'family :age 89 :name "John") *members*)
(push (make-instance 'family :age 14 :name "Mike") *members*)

;;; define a method to retrieve all slot value by a given slot 
(defmethod find-name (x)  
  (describe (find x *members* :key #'name :test #'string-equal)))

question:

How can I define a method, by which "PUSH" the instancs to a hashtable
can be automaticly realized? eg.:
when I type (make-instance ......), it get the same result as the
(push (make-instance....) *members*)

I guess it may be possible by using a :after or :around method. But as
a biginner, it is realy too hard for me.

From: Barry Margolin
Subject: Re: Can anyone show a beginner a short code as solution? CLOS
Date: 
Message-ID: <Fjmo9.28$LA4.3571@paloalto-snr1.gtei.net>
In article <····························@posting.google.com>,
Jacktian <···········@yahoo.co.uk> wrote:
>How can I define a method, by which "PUSH" the instancs to a hashtable
>can be automaticly realized? eg.:
>when I type (make-instance ......), it get the same result as the
>(push (make-instance....) *members*)
>
>I guess it may be possible by using a :after or :around method. But as
>a biginner, it is realy too hard for me.

(defmethod shared-initialize ((self family) slot-names &rest initargs)
  (declare (ignore slot-names initargs))
  (pushnew self *members*))

-- 
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: Marco Antoniotti
Subject: Re: Can anyone show a beginner a short code as solution? CLOS
Date: 
Message-ID: <y6cbs66hrrg.fsf@octagon.valis.nyu.edu>
Barry Margolin <······@genuity.net> writes:

> In article <····························@posting.google.com>,
> Jacktian <···········@yahoo.co.uk> wrote:
> >How can I define a method, by which "PUSH" the instancs to a hashtable
> >can be automaticly realized? eg.:
> >when I type (make-instance ......), it get the same result as the
> >(push (make-instance....) *members*)
> >
> >I guess it may be possible by using a :after or :around method. But as
> >a biginner, it is realy too hard for me.
> 
> (defmethod shared-initialize ((self family) slot-names &rest initargs)
>   (declare (ignore slot-names initargs))
>   (pushnew self *members*))

Why the primary method on SHARED-INITIALIZE?  Wouldn't an :AFTER
method on INITIALIZE-INSTANCE better?


;;; Untested.

(defvar *members* (make-hash-table :test #'equalp))

(defmethod initialize-instance :after ((f family) &key (name ""))
  ;; Here you could check that NAME is reasonable.
  (setf (gethash name *members*) f))

Cheers

-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
715 Broadway 10th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.
From: Francis Leboutte
Subject: Re: Can anyone show a beginner a short code as solution? CLOS
Date: 
Message-ID: <tkd5qu4rdkaim32uti6tu6i2f6umrh593u@4ax.com>
Marco Antoniotti <·······@cs.nyu.edu> wrote:

>
>Barry Margolin <······@genuity.net> writes:
>
>> In article <····························@posting.google.com>,
>> Jacktian <···········@yahoo.co.uk> wrote:
>> >How can I define a method, by which "PUSH" the instancs to a hashtable
>> >can be automaticly realized? eg.:
>> >when I type (make-instance ......), it get the same result as the
>> >(push (make-instance....) *members*)
>> >
>> >I guess it may be possible by using a :after or :around method. But as
>> >a biginner, it is realy too hard for me.
>> 
>> (defmethod shared-initialize ((self family) slot-names &rest initargs)
>>   (declare (ignore slot-names initargs))
>>   (pushnew self *members*))
>
>Why the primary method on SHARED-INITIALIZE?  Wouldn't an :AFTER
>method on INITIALIZE-INSTANCE better?
>
>;;; Untested.
>
>(defvar *members* (make-hash-table :test #'equalp))
>
>(defmethod initialize-instance :after ((f family) &key (name ""))
>  ;; Here you could check that NAME is reasonable.
>  (setf (gethash name *members*) f))

an initialize-instance after method looks better because it's not
useful to reinitialize the hashtable entry each time the class
definition is updated (that's why pushnew is needed instead of push in
the shared-initialize AFTER method, if a list is used).

>Cheers

--
Francis Leboutte
 www.algo.be
 Logo programming: http://www.algo.be/logo.html
From: Barry Margolin
Subject: Re: Can anyone show a beginner a short code as solution? CLOS
Date: 
Message-ID: <ITBo9.4$B01.354@paloalto-snr1.gtei.net>
In article <··································@4ax.com>,
Francis Leboutte  <··········@algo.be> wrote:
>Marco Antoniotti <·······@cs.nyu.edu> wrote:
>
>>
>>Barry Margolin <······@genuity.net> writes:
>>
>>> In article <····························@posting.google.com>,
>>> Jacktian <···········@yahoo.co.uk> wrote:
>>> >How can I define a method, by which "PUSH" the instancs to a hashtable
>>> >can be automaticly realized? eg.:
>>> >when I type (make-instance ......), it get the same result as the
>>> >(push (make-instance....) *members*)
>>> >
>>> >I guess it may be possible by using a :after or :around method. But as
>>> >a biginner, it is realy too hard for me.
>>> 
>>> (defmethod shared-initialize ((self family) slot-names &rest initargs)
>>>   (declare (ignore slot-names initargs))
>>>   (pushnew self *members*))
>>
>>Why the primary method on SHARED-INITIALIZE?  Wouldn't an :AFTER
>>method on INITIALIZE-INSTANCE better?
>>
>>;;; Untested.
>>
>>(defvar *members* (make-hash-table :test #'equalp))
>>
>>(defmethod initialize-instance :after ((f family) &key (name ""))
>>  ;; Here you could check that NAME is reasonable.
>>  (setf (gethash name *members*) f))
>
>an initialize-instance after method looks better because it's not
>useful to reinitialize the hashtable entry each time the class
>definition is updated (that's why pushnew is needed instead of push in
>the shared-initialize AFTER method, if a list is used).

I put it on shared-initialize so that it would also be run during
change-class.

-- 
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: Francis Leboutte
Subject: Re: Can anyone show a beginner a short code as solution? CLOS
Date: 
Message-ID: <tr16quos7hhvhkl9tneiqdsf97807vivti@4ax.com>
Barry Margolin <······@genuity.net> wrote:

>In article <··································@4ax.com>,
>Francis Leboutte  <··········@algo.be> wrote:
>>Marco Antoniotti <·······@cs.nyu.edu> wrote:
>>>
>>>Why the primary method on SHARED-INITIALIZE?  Wouldn't an :AFTER
>>>method on INITIALIZE-INSTANCE better?
>>>
>>>(defvar *members* (make-hash-table :test #'equalp))
>>>
>>>(defmethod initialize-instance :after ((f family) &key (name ""))
>>>  ;; Here you could check that NAME is reasonable.
>>>  (setf (gethash name *members*) f))
>>
>>an initialize-instance after method looks better because it's not
>>useful to reinitialize the hashtable entry each time the class
>>definition is updated (that's why pushnew is needed instead of push in
>>the shared-initialize AFTER method, if a list is used).
>
>I put it on shared-initialize so that it would also be run during
>change-class.

I see. When an instance changes class, if it is kept in both
containers (variables like *members*), this seems the way to go.
However if the instance has to be removed from its first container,
using an initialize-instance after method may be a good solution too
(as it is anyway necessary to define a change-class after method to
update the container)


--
Francis Leboutte
 www.algo.be
 Logo programming: http://www.algo.be/logo.html
From: Marco Antoniotti
Subject: Re: Can anyone show a beginner a short code as solution? CLOS
Date: 
Message-ID: <y6cvg4ch897.fsf@octagon.valis.nyu.edu>
Barry Margolin <······@genuity.net> writes:

> In article <··································@4ax.com>,
> Francis Leboutte  <··········@algo.be> wrote:
> >Marco Antoniotti <·······@cs.nyu.edu> wrote:
        ...

> >>(defmethod initialize-instance :after ((f family) &key (name ""))
> >>  ;; Here you could check that NAME is reasonable.
> >>  (setf (gethash name *members*) f))
> >
> >an initialize-instance after method looks better because it's not
> >useful to reinitialize the hashtable entry each time the class
> >definition is updated (that's why pushnew is needed instead of push in
> >the shared-initialize AFTER method, if a list is used).
> 
> I put it on shared-initialize so that it would also be run during
> change-class.

Excellent point.  I did not think of that.  Thanks

Cheers


-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
715 Broadway 10th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.
From: Duane Rettig
Subject: Re: Can anyone show a beginner a short code as solution? CLOS
Date: 
Message-ID: <4adloyk34.fsf@beta.franz.com>
Francis Leboutte <··········@algo.be> writes:

> Marco Antoniotti <·······@cs.nyu.edu> wrote:
> 
> >
> >Barry Margolin <······@genuity.net> writes:
> >
> >Why the primary method on SHARED-INITIALIZE?  Wouldn't an :AFTER
> >method on INITIALIZE-INSTANCE better?
> >
> >;;; Untested.
> >
> >(defvar *members* (make-hash-table :test #'equalp))
> >
> >(defmethod initialize-instance :after ((f family) &key (name ""))
> >  ;; Here you could check that NAME is reasonable.
> >  (setf (gethash name *members*) f))
> 
> an initialize-instance after method looks better because it's not
> useful to reinitialize the hashtable entry each time the class
> definition is updated (that's why pushnew is needed instead of push in
> the shared-initialize AFTER method, if a list is used).

It is clear that an after method is good.  Whether the better
place to put the after method is on initialize-instance or on
shared-initialize depends on several factors.  This includes how
often you intend to change the class of the instance (either by
modifying the class or by switching the instance to a different
class) - the more often the more liekly it is that
initialize-instance is the right one, but the less often might
also point toward shared-initilize.  It also depend on whether
you intend to use some sort of resourcing mechanism for these
instances; such mechamisms need reinitialization instructions,
and so you would either have to duplicate your initialize-instance
after method in a reinitilize-instance after method, or just use
a shared-initialize after method to kill two birds with one stone
(even if you never plan to change the instance's class).

There are probably other factors, but these two are the ones that
stand out most in my mind.

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: Barry Margolin
Subject: Re: Can anyone show a beginner a short code as solution? CLOS
Date: 
Message-ID: <Esmo9.30$LA4.3559@paloalto-snr1.gtei.net>
In article <·················@paloalto-snr1.gtei.net>,
Barry Margolin  <······@genuity.net> wrote:
>In article <····························@posting.google.com>,
>Jacktian <···········@yahoo.co.uk> wrote:
>>How can I define a method, by which "PUSH" the instancs to a hashtable
>>can be automaticly realized? eg.:
>>when I type (make-instance ......), it get the same result as the
>>(push (make-instance....) *members*)
>>
>>I guess it may be possible by using a :after or :around method. But as
>>a biginner, it is realy too hard for me.
>
>(defmethod shared-initialize ((self family) slot-names &rest initargs)
>  (declare (ignore slot-names initargs))
>  (pushnew self *members*))

Oops, that should be an :AFTER method, not a primary method.

-- 
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: Wade Humeniuk
Subject: Re: Can anyone show a beginner a short code as solution? CLOS
Date: 
Message-ID: <Qpmo9.35508$OO4.2296930@news1.telusplanet.net>
"Jacktian" <···········@yahoo.co.uk> wrote in message
·································@posting.google.com...
> original code :
>
> (defvar *members* '()
>     "List of all members.")
>
> ;; define a class
> (defclass family () ((age :accessor age
>                           :initarg :age
>                           :initform nil)
>                        (name :accessor name
>                                :initarg :name
>                                :initform nil)))
>
> ;;; build up instances and push to hash table
> (push (make-instance 'family :age 89 :name "John") *members*)
> (push (make-instance 'family :age 14 :name "Mike") *members*)

> How can I define a method, by which "PUSH" the instancs to a hashtable
> can be automaticly realized? eg.:
> when I type (make-instance ......), it get the same result as the
> (push (make-instance....) *members*)
>
> I guess it may be possible by using a :after or :around method. But as
> a biginner, it is realy too hard for me.

Its not too hard, it is also the correct guess.

(defmethod initialize-instance :after ((inst family) &rest initargs)
   (declare (ignore initargs))
   (push inst *members*))

Wade