Newsgroups: comp.lang.lisp
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!news.sei.cmu.edu!cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!cs.utexas.edu!bcm!news.msfc.nasa.gov!news.larc.nasa.gov!saimiri.primate.wisc.edu!aplcenmp!hall
From: hall@aplcenmp.apl.jhu.edu (Marty Hall)
Subject: Re: Processing of inputs
Message-ID: <Cx7sxF.EpL@aplcenmp.apl.jhu.edu>
Organization: JHU/APL AI Lab, Hopkins P/T CS Faculty
References: <36ui4g$8ap@newsflash.concordia.ca>
Date: Wed, 5 Oct 1994 19:32:03 GMT
Lines: 42

In article <36ui4g$8ap@newsflash.concordia.ca> iain@ece.concordia.ca (Iain J. Bryson) writes:
>Hi, I am new to LISP and CLOS and I have stumbled
>across a problem.  Is there any way to write your
>own writer function which could process the new
>value of a slot or field before it sets that
>field or slot's value?  I mean, for instance, I
>want to make a time class and I don't want to
>be able to set hours to 6765 etc..

If you're doing SETF on the SLOT-VALUE, there is no simple way. But
assuming you are using an accessor, you have two main options:

(A) Completely rewrite (override) the SETF method on that accessor.

(B) Write an around method and leave the original SETF method alone.

Here is an example that does both (you'd really want to do one or the
other, not both, most likely).

;;;======================================================================
;;; Example class.

(defclass Time ()
  ((Hours :accessor hours :initarg :Hours :initform 0)))

;;; Override the normal SETF method that :accessor gave you.

(defmethod (setf Hours) ((New-Hours number) (Time-Object Time))
  (setf (slot-value Time-Object 'Hours)
	(min New-Hours 100)))

;;; Add an around method to the existing SETF method.

(defmethod (setf Hours) :around ((New-Hours number) (Time-Object Time))
  (if (>= New-Hours 0)
      (call-next-method)
      (error "Can't have a negative time...")))

;;;======================================================================

						- Marty
(proclaim '(inline skates))
