CLOS: How to make a slot have an enforced type of vector of symbols?
Asked Answered
F

2

5

I'm trying to create a class that can store a vector of symbols in a slot in SBCL. I cannot figure out how to set it up.

My best guess thus far has been

(defclass Individual ()
  ((discrete-decisions :type (vector symbol))))

This returns the following error:

keyword argument not a symbol:
(DISCRETE-DECISIONS :TYPE (VECTOR SYMBOL)).
   [Condition of type SB-INT:SIMPLE-PROGRAM-ERROR]

Some experimenting has shown that changing the type to just symbol returns the same error. I thought that symbol was a valid type in Common Lisp... am I mistaken?

How can I get this to work?

[EDIT]

The above problem I had was running SBCL 1.0.58 in the 09-22-2012 Slime build under Emacs 24.2. When I run SBCL 1.0.58 from the command line, there is no problem. This doesn't seem like an SBCL issue...

Fornof answered 23/9, 2012 at 21:41 Comment(4)
btw., in default safety settings SBCL will not check slot typesCaptivity
I'm using SBCL 1.0.58 on Windows 7. Might this be a bug?Fornof
could be, you should consult the sbcl mailing listCaptivity
The spec says that the effect of storing values of another type in the slot is undefined. So it's not 'portable' to expect the type to be checked. An alternative is to leave the type unspecified and test yourself using check-type.Francisfrancisca
D
4

You might consider defining an :after method on slot accessor/writer. Also, the ultimate degree of control can be exercised by defining your own metaclass and customization of slot-value-using-class

Daytime answered 17/5, 2013 at 6:37 Comment(1)
Or use :before. For example, for the class cell with slot genome, we can ensure that the type of the object in the genome slot matches that specified in the class definition: (defmethod (setf cell-genome) :before (new-value (object cell)) (let ((sd-genome (slot-definition-for-cell-genome))) (assert (typep new-value (sb-mop:slot-definition-type sd-genome)))))Battement
E
3

I know I'm probably too late, but you should wrap around your classe declaration with a optimization for safety. For example:

(locally (declare (optimize safety))
    (defclass test-class ()
      ((some-slot :type real :initarg :some-slot :accessor :test-some-slot))))
Edomite answered 3/5, 2013 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.