Checking consistency between class slots

Discussion of Common Lisp
Post Reply
djpeebz
Posts: 2
Joined: Sun May 22, 2011 6:40 am

Checking consistency between class slots

Post by djpeebz » Sun May 22, 2011 6:52 am

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))

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Checking consistency between class slots

Post by ramarren » Sun May 22, 2011 10:23 am

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.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Checking consistency between class slots

Post by nuntius » Sun May 22, 2011 12:58 pm

see also the Object Initialization section of http://www.gigamonkeys.com/book/object- ... asses.html

djpeebz
Posts: 2
Joined: Sun May 22, 2011 6:40 am

Re: Checking consistency between class slots

Post by djpeebz » Thu May 26, 2011 2:48 pm

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

Post Reply