From: Frederick C. Gibson, Architect
Subject: Where is the constructor?
Date: 
Message-ID: <39b5e7ca.1039004@news.pacbell.net>
In the definition of a class, how do I specify that a particular
method is called upon creation?  Basically, I want a constructor as in
Java or C++.  When one slot is set by a value passed to make-instance,
I want to set another slot in the superclass automatically.

Thanks.

From: Joachim Achtzehnter
Subject: Re: Where is the constructor?
Date: 
Message-ID: <m2snre2dnk.fsf@wizard.kraut.bc.ca>
·········@gibson-design.com (Frederick C. Gibson, Architect) writes:
>
> In the definition of a class, how do I specify that a particular
> method is called upon creation?  Basically, I want a constructor as in
> Java or C++.  When one slot is set by a value passed to make-instance,
> I want to set another slot in the superclass automatically.

In Java and C++ constructors are special member functions. In CLOS
they are not special in any way. You can write an ordinary function
and use it as a constructor. Give it a recognizable name like make-foo
to construct a foo instance.

Joachim

-- 
work:     ········@realtimeint.com (http://www.realtimeint.com)
private:  ·······@kraut.bc.ca      (http://www.kraut.bc.ca)
From: Samir Sekkat
Subject: Re: Where is the constructor?
Date: 
Message-ID: <MPG.14202320dda9b396989688@news.compuserve.com>
In article <················@news.pacbell.net>, ·········@gibson-
design.com says...
> In the definition of a class, how do I specify that a particular
> method is called upon creation?  Basically, I want a constructor as in
> Java or C++.  When one slot is set by a value passed to make-instance,
> I want to set another slot in the superclass automatically.
> 
> Thanks.
> 
BTW: any other tips about constructors will be greatly appreciated!!!

if the slot in the superclass can be set without knowledge about other 
slots AND do not need access to the owner object, then you can use the 
:initform option:

(defclass my-superclass ()
  ((slot-to-be-set :accessor slot-to-be-set
                   :initform 'bla)))

(slot-to-be-set (make-instance 'my-superclass))

returns 
BLA

For more flexibility, you can access the instance after the creation with 
an after-method on initialize-instance:

(defmethod initialize-instance :after ((obj my-superclass) &rest 
initargs)
  (describe obj))

(slot-to-be-set (make-instance 'my-superclass))

returns

#<MY-SUPERCLASS @ #x20bf78f2> is an instance of #<STANDARD-CLASS MY-
SUPERCLASS>:
 The following slots have :INSTANCE allocation:
  SLOT-TO-BE-SET   BLA
BLA

The after method of the superclass will be started even by the creation 
of subclasses...
From: Marco Antoniotti
Subject: Re: Where is the constructor?
Date: 
Message-ID: <y6cbsy1o8u5.fsf@octagon.mrl.nyu.edu>
·········@gibson-design.com (Frederick C. Gibson, Architect) writes:

> In the definition of a class, how do I specify that a particular
> method is called upon creation?  Basically, I want a constructor as in
> Java or C++.  When one slot is set by a value passed to make-instance,
> I want to set another slot in the superclass automatically.

You need to look at the standard method INITIALIZE-INSTANCE (and maybe
SHARED-INITIALIZE).  You do this by defining :after (and maybe
:before) methods.

CLOS has a "instance creation protocol" which you can tweak.

Here is the example (untested)

(defclass parent () ((s :accessor parent-s)))

(defclass child (parent)
 ((my-slot :accessor child-slot :initarg :slot))
 (:default-initargs :slot 123))


(defmethod initialize-instance :after ((c child-1) &key &allow-other-keys)
  (setf (parent-s c) (* (child-slot c) pi)))

or

(defmethod initialize-instance :after ((c child-1) &key (slot 123))
  (setf (parent-s c) (* slot pi)))

Hope it helps

Cheers

-- 
Marco Antoniotti =============================================================
NYU Bioinformatics Group			 tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                          fax  +1 - 212 - 995 4122
New York, NY 10003, USA				 http://galt.mrl.nyu.edu/valis