下面的代碼時指定一個槽值作爲重點做什麼,我想:刪除重複
1 (defclass some-class()
2 ((some-slot
3 :initarg :somearg
4 :initform (error ":somearg not specified"))))
5 (defparameter *alpha* (make-instance 'some-class :somearg 3))
6 (defparameter *beta* (make-instance 'some-class :somearg 5))
7 (defparameter *gamma* (make-instance 'some-class :somearg 3))
8 (princ (slot-value *beta* 'some-slot)) (terpri)
9 (defparameter *delta* (list *alpha* *beta* *gamma*))
10 (princ *delta*) (terpri)
11 (princ (remove-duplicates *delta*
12 :test #'equal
13 :key (lambda (x) (slot-value x 'some-slot))))
14 (terpri)
5
(#<SOME-CLASS #x21C1D71E> #<SOME-CLASS #x21C1DAFE> #<SOME-CLASS #x21C1DC3E>)
(#<SOME-CLASS #x21C1DAFE> #<SOME-CLASS #x21C1DC3E>)
但是,有沒有辦法做到這一點,而無需編寫第13行的功能?有沒有一種簡捷的方式來指定類實例中的槽值?
當然,以下是語法錯誤,但它給出了我正在尋找的東西的一般概念。
1 (princ (remove-duplicates *delta*
2 :test #'equal
3 :key '(slot-value 'some-slot)))
4 (terpri)
*** - FUNCALL: (SLOT-VALUE 'SOME-SLOT) is not a function name; try using a
symbol instead
Inaimathi,我設置的原因:在initForm錯誤是觸發一個錯誤不當插槽被訪問時,但更早,當對象被創建時。這是爲我做的嗎? – 2012-01-31 05:25:08
@Bill - 啊,我明白了。不,默認行爲是在訪問未設置的插槽時發生錯誤。你知道了。 – Inaimathi 2012-01-31 13:33:01