Subject: Re: [Q] Limitation of variables?
From: Erik Naggum <erik@naggum.net>
Date: Fri, 11 May 2001 20:10:19 GMT
Newsgroups: comp.lang.lisp
Message-ID: <3198600534513378@naggum.net>

* Sungwoo Lim
> Actual codes are as below.
> 
> (defstruct stroke-info
>   (first-x   0.0   :type double-float)
>   (first-y   0.0   :type double-float)
>    ........
>   (whatever  0     :type integer))
> 
> (setq a1 (make-stroke-info)
> (defun put-stroke-info (a1 ... a93)
>   (setf (stroke-info-first-x a1)  a2
>         (stroke-info-first-y a1)  a3
>           ........
>         (stroke-info-whatever a1) a93))

  Does this imply that you reuse the instance assigned in a1?  I think that
  is fairly bad style.  What if somebody kept a pointer to that instance?
  What you do here requires an "ownership protocol" such that those who
  think they might want to retain a pointer to the object longer than the
  next instance, will have to make a copy.  Violations of this protocol
  means that some user of the instance will find that the data changes.

  I suggest that you create a new instance every time you have a "stroke"
  and instead of this "put-stroke-info", use the boa constructor option to
  defstruct to make all of this (much, MUCH) more palatable.

(defstruct (stroke-info
            (:constructor make-stroke-info (first-x first-y ... whatever)))
  (first-x   0.0   :type double-float)
  (first-y   0.0   :type double-float)
  ...
  (whatever  0     :type integer))

  Please note that the list of arguments to make-stroke-info (constructor)
  may contain complex argument relationships and arbitrary computations.
  The boa constructor assigns values to slots By Order of Arguments, and
  you can use the value of previously specified arguments and even the
  presence of such arguments in the boa lambda list.

  Note also that the list of arguments to the simplest boa constructor may
  be computed from the defstruct body, which a little snippet of Emacs Lisp
  code can do for you if you do not want to write your own simple macro.

  Also note that you defstruct by default makes a copy-stroke-info function
  that copies every slot value from one instance to another such that the
  slots are eql after the copy.

#:Erik
-- 
  Travel is a meat thing.