Page 1 of 1

Checking consistency between class slots

Posted: Sun May 22, 2011 6:52 am
by djpeebz
Hi,

I want to check that the value of a slot in a class (defined when the class is created) is appropriate based on the values of other slots (e.g., in the example below that the value val is within the range of min and max) and to produce an error if this is not the case. I've not been able to find a solution so far. I'd be very grateful for advice. Thank you.

(defclass foo ()
((min :initarg :min)
(max :initarg :max)
(val :initarg :val))

Re: Checking consistency between class slots

Posted: Sun May 22, 2011 10:23 am
by ramarren
You can enforce such checks when setting the value by creating custom accessor methods. Possibly as an auxiliary method. You can add such a check at instance creation time by adding an :after method to SHARED-INITIALIZE.

More detailed description of using CL object system is in a book by Sonya Keene. Unfortunately it is not available for free on the net.

Re: Checking consistency between class slots

Posted: Sun May 22, 2011 12:58 pm
by nuntius
see also the Object Initialization section of http://www.gigamonkeys.com/book/object- ... asses.html

Re: Checking consistency between class slots

Posted: Thu May 26, 2011 2:48 pm
by djpeebz
Many thanks to you both. I have managed to develop a solution using (defmethod initialize-instance :after ...) My problem arose because I misunderstood how this worked.

Thanks again