1

How can I enforce slot's type in CLOS.

I know that (locally (declare (optimize safety))) (defclass foo () ((num :type number... could be answer, but I think it's not portable enough. (only with SBCL, not CLISP 2.49)

Or, should I implement those things? (for check each slots following specified :types.)

  1. defmethod :after (setf SLOT).
  2. defmethod :after initialize-instance.

Is there any simpler way to do that?

And, is there any "MOP" or metaclass something for hook "slot-updates"?

Thanks.

EDIT: I think it's not the same question with CLOS: How to make a slot have an enforced type of vector of symbols?, because I need more informations about that "metaclass" or slot-value-with-class things.

EDIT: I found closer-mop provides preferrable, portable-way.

CL-USER> (nth 0 (closer-mop:class-slots (find-class 'foo-class)))
CL-USER> (closer-mop:slot-definition-type *)
Community
  • 1
  • 1
  • `(locally (declare (optimize safety)))` won't provide an answer. An implementation can discard type declarations, so optimizing for safety won't guarantee a check. If you absolutely want to guarantee that the value has a particular type, you're going to have to get into the methods for setting the slot's values. – Joshua Taylor Oct 01 '14 at 14:27
  • Yes, I know that. Please read 'all of them'. I was question about "that" too. Thanks. – Jong-Hyouk Yun Oct 01 '14 at 14:34
  • Yes, and since you wrote (emphasis added) "I know that ... *could* be an answer, but I *think* it's not portable enough," I was commenting in support to your hypothesis. I did read the whole question. – Joshua Taylor Oct 01 '14 at 14:36
  • Actually, English is not my mother tongue, at all. I was misunderstood your intentions & nuance, sorry again. :-) – Jong-Hyouk Yun Oct 01 '14 at 14:41
  • No worries, and no harm done. I think your other hypothesis (that you'll need to get into `:after` methods on the setting methods is probably correct. You'll have to be careful about all the ways to set the value, though (e.g., setf slot-value). – Joshua Taylor Oct 01 '14 at 14:43
  • Thanks. I'm looking for something like a `*update-slot-hook*` auto'mize -or- macro'mize that. Actually, I'm not expecting something like that. but I think there should be other way to do, it's very primitive thing, a type-checking for instance's slot. I think. – Jong-Hyouk Yun Oct 01 '14 at 14:47

0 Answers0