This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Checking consistency between class slots

4 posts · 1716 views

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

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

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