CLOS: How to make a slot have an enforced type of

2019-02-24 02:54发布

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

2条回答
叛逆
2楼-- · 2019-02-24 03:28

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))))
查看更多
该账号已被封号
3楼-- · 2019-02-24 03:46

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

查看更多
登录 后发表回答