how to make a "cacheing" slot

From: Brian Denheyer <>
Date: Sun, 28 Nov 1999 22:19:11 -0800 (PST)

Here's the situation :

(define-class <foo> ()

descriptive-value is a value which is meaningful to the user such as
%. calc-value is a value used internally which is calculated from
descriptive-value. It only needs to be recalculated when
descriptive-value changes.

My first thought was to make descriptive-value a virtual slot with a
setter which will update calc-value. Since there is an actual value
associated with descriptive-value it doesn't make sense to do this.

So the only plan I've come up with so far is to have 3 slots. once
holds descriptive-value, one holds calc-value and the third is a
virtual slot which is used to access descriptive-value and forces
calc-value to be updated. Here's my solution :

(define-class <foo> ()
((descriptive-value :accessor descriptive-value)
 (calc-value :accessor calc-value)
 (v-value :slot-set! (lambda (self value)
                       (set! (descriptive-value self) value)
                       (set! (calc-value
                              (calculation-of-some-sort value))))
          :slot-ref (lambda (self)
                     (calc-value self)))))

So in effectm calc-value should really be called "cache-value".

For some reason I think there should be a better way. For one thing
if you have to do this 2 or 3 times in a class it's sort of tedious
keeping all the slot names straight.

It turns out I run into this on a fairly regular basis, so I'm looking
for a more elegant/uniform solution.

Any ideas ?



P.S. For a 100pt bonus, what's a good way to do this if "cache-value"
depends on more than 1 slot...
Received on Mon Nov 29 1999 - 07:19:00 CET

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST