From: David Hanley
Subject: CLOS question
Date: 
Message-ID: <390F5626.EF07EA8C@ncgr.org>
Can I do something like this:

> (declass foo ()
     (a :accessor a :intform (* x 2)))

> (setq f (make-instance 'foo x 4))

> (a f)

> 8

?

Yes, I can use special variables, but I'd like something a tad less
gross.

dave

From: Tunc Simsek
Subject: Re: CLOS question
Date: 
Message-ID: <390F6EFE.E99ED67D@robotics.eecs.berkeley.edu>
I guess that you mean:

(defclass foo ()
  ((x :initarg x)
   (a :accessor a :initform (* x 2)))

 (:metaclass ordered-init-class))

In which case you may implement an after, or replacement method on
INITIALIZE-INSTANCE, which may be tough (you may have to take
into account inheritance etc ...)

Tunc

David Hanley wrote:
> 
> Can I do something like this:
> 
> > (declass foo ()
>      (a :accessor a :intform (* x 2)))
> 
> > (setq f (make-instance 'foo x 4))
> 
> > (a f)
> 
> > 8
> 
> ?
> 
> Yes, I can use special variables, but I'd like something a tad less
> gross.
> 
> dave
From: Keke Abe
Subject: Re: CLOS question
Date: 
Message-ID: <keke-0305001127230001@tc-1-216.osaka.gol.ne.jp>
In article <·················@ncgr.org>, 
David Hanley <···@ncgr.org> wrote:

> Can I do something like this:
> 
> > (declass foo ()
>      (a :accessor a :intform (* x 2)))
> 
> > (setq f (make-instance 'foo x 4))
> 
> > (a f)
> 
> > 8

(defclass foo ()
  ((a :initarg :a :accessor a)))

(defmethod initialize-instance ((self foo) &rest keys &key (x 0))
  (declare (dynamic-extent keys))
  (apply #'call-next-method
         self
         :a (* x 2)
         keys))

? (setq f (make-instance 'foo :x 4))
#<FOO #x96F5046>
? (a f)
8
? 
                                
I often write codes like this. I'd like to know if there's a better
way.

regards,
abe
From: Alexander Clark
Subject: Re: CLOS question
Date: 
Message-ID: <lfe0hsojodk48fec3fvmeapfi0b70gnf2o@4ax.com>
On Wed, 03 May 2000 02:28:27 GMT, ····@gol.com (Keke Abe) wrote:

>In article <·················@ncgr.org>, 
>David Hanley <···@ncgr.org> wrote:
>
>> Can I do something like this:
>> 
>> > (declass foo ()
>>      (a :accessor a :intform (* x 2)))
>> 
>> > (setq f (make-instance 'foo x 4))
>> 
>> > (a f)
>> 
>> > 8
>
>(defclass foo ()
>  ((a :initarg :a :accessor a)))
>
>(defmethod initialize-instance ((self foo) &rest keys &key (x 0))
>  (declare (dynamic-extent keys))
>  (apply #'call-next-method
>         self
>         :a (* x 2)
>         keys))
>
>? (setq f (make-instance 'foo :x 4))
>#<FOO #x96F5046>
>? (a f)
>8
>? 
>                                
>I often write codes like this. I'd like to know if there's a better
>way.
>
>regards,
>abe

I tend to use

(defun make-foo (x)
	(make-instance 'foo
	:x x
	:a (* 2 x)))
From: David Hanley
Subject: Re: CLOS question
Date: 
Message-ID: <39107C06.221856EA@ncgr.org>
Alexander Clark wrote:

> On Wed, 03 May 2000 02:28:27 GMT, ····@gol.com (Keke Abe) wrote:
>
> >In article <·················@ncgr.org>,
> >David Hanley <···@ncgr.org> wrote:
> >
> >> Can I do something like this:
> >>
> >> > (declass foo ()
> >>      (a :accessor a :intform (* x 2)))
> >>
> >> > (setq f (make-instance 'foo x 4))
> >>
> >> > (a f)
> >>
> >> > 8
> >
> >(defclass foo ()
> >  ((a :initarg :a :accessor a)))
> >
> >(defmethod initialize-instance ((self foo) &rest keys &key (x 0))
> >  (declare (dynamic-extent keys))
> >  (apply #'call-next-method
> >         self
> >         :a (* x 2)
> >         keys))
> >
> >? (setq f (make-instance 'foo :x 4))
> >#<FOO #x96F5046>
> >? (a f)
> >8
> >?
> >
> >I often write codes like this. I'd like to know if there's a better
> >way.
> >
> >regards,
> >abe
>
> I tend to use
>
> (defun make-foo (x)
>         (make-instance 'foo
>         :x x
>         :a (* 2 x)))

Unfortunately, neither of these work terribly well for me, because
i'm not calling defclass directly--it's being done via macro.
Specifically,
it's in lispworks for windows:

(define-interface dummy-window
  (:panels
    (close-button button :callback close-botton-action)))

This expands into a defclass.  The problem is, I may want to have more
than one version of that window active, so one version of close-window
won't work.  I more-or-less do this:

(declaim (special close-button-action))

(defun make-ui()
  (let ((ui nil))
         (close-button-action #'(lambda(arg-1 arg-2) (destroy-interface
ui)))))
    (setq ui (make-instance 'dummy window))))

I hope that, with the above define-interface, what I'm doing isn't
totally obscure and stupid.  As you can see, unless I want to totally
skip define-interface, I can't construct it and set fields.

For what the two of you seem to be doing, The following works:

(let ((x 1)(y 1))
              (defun make-foo( x1 y1 )
                (setf x x1 y y1)
                (make-instance 'foo))
              (defclass foo ()
                ((xv :accessor xv :initform x)
                 (yv :accessor yv :initform y))))

Without using pukey globals or special variables.  Obviously, the
initforms can be more complex and even call enclosed functions that
use those variables, etc.

dave
From: Tim Moore
Subject: Re: CLOS question
Date: 
Message-ID: <8eq52c$6t5$0@216.39.145.192>
On Wed, 3 May 2000, David Hanley wrote:

> 
> 
> Alexander Clark wrote:
> 
> > On Wed, 03 May 2000 02:28:27 GMT, ····@gol.com (Keke Abe) wrote:
> > >(defclass foo ()
> > >  ((a :initarg :a :accessor a)))
> > >
> > >(defmethod initialize-instance ((self foo) &rest keys &key (x 0))
> > >  (declare (dynamic-extent keys))
> > >  (apply #'call-next-method
> > >         self
> > >         :a (* x 2)
> > >         keys))
> > >
> > >? (setq f (make-instance 'foo :x 4))
> > >#<FOO #x96F5046>
> > >? (a f)
> > >8
> > >?
> > >
> > >I often write codes like this. I'd like to know if there's a better
> > >way.
I don't know if this is better, but to my mind it looks nicer:
(defmethod initialize-instance :after ((self foo) &key (x 0)
				       &allow-other-keys)
  (setf (slot-value self 'a) (* x 2)))

It has different semantics in that the new value of a isn't visible to the
superclass initialize-instance methods, which may or may not be a problem.

> > I tend to use
> >
> > (defun make-foo (x)
> >         (make-instance 'foo
> >         :x x
> >         :a (* 2 x)))
> 
> Unfortunately, neither of these work terribly well for me, because
> i'm not calling defclass directly--it's being done via macro.
> Specifically,
> it's in lispworks for windows:
> 
> (define-interface dummy-window
>   (:panels
>     (close-button button :callback close-botton-action)))
> 
> This expands into a defclass.  The problem is, I may want to have more
> than one version of that window active, so one version of close-window
> won't work.  I more-or-less do this:
> (declaim (special close-button-action))
> 
> (defun make-ui()
>   (let ((ui nil))
>          (close-button-action #'(lambda(arg-1 arg-2) (destroy-interface
> ui)))))
>     (setq ui (make-instance 'dummy window))))
> 
> I hope that, with the above define-interface, what I'm doing isn't
> totally obscure and stupid.  As you can see, unless I want to totally
> skip define-interface, I can't construct it and set fields.
If you knew how to get at the callback slot of the panel in the object,
you could write:
(defmethod initialize-instance :after ((self dummy-window)
				       &allow-other-keys)
  (setf (callback-place self) #'(lambda (arg1 arg2)
				  (destroy-interface self))))

Is that information hidden?

Tim