From: Kent M Pitman
Subject: Re: defstruct with :constructor
Date: 
Message-ID: <sfwzpi4g28m.fsf@world.std.com>
Sam Steingold <ยทยทยท@usa.net> writes:

> My problem is the following: I have a structure with one of the fields
> being computable from the rest, e.g
> (defstruct triangle
>  (side1 0.0 :type double-float)
>  (side2 0.0 :type double-float)
>  (side3 0.0 :type double-float)
>  (area 0.0 :type double-float))

I usually do:

(defstruct triangle 
  (side1 0.0 :type double-float) 
  (side2 0.0 :type double-float) 
  (side3 0.0 :type double-float)
  (area-internal nil))

(defun triangle-area (triangle)
  (or (triangle-area-internal triangle)
      (setf (triangle-area-internal triangle) ...)))

with appropriate type and/or inline declarations for triangle-area as needed,
as well as either a check-type or declaration of the triangle arg to triangle.
But the basic approach is hopefully clear.

Of course, these approaches assume you neer side-effect side1, side2,
and side3 since you otherwise need to cache the values.  But one nice
thing about the above an side-effecting is that youc an just bash
area-internal back to nil, you don't have to recompute the area on each
change to the triangle, only on each request for its area where it's not
already known.
 --Kent