【发布时间】:2016-02-23 01:01:29
【问题描述】:
我有以下课程:
(defclass category ()
((cat-channel-name
:accessor cat-channel-name :initarg :cat-channel-name :initform "" :type string
:documentation "Name of the channel of this category")
(cat-min
:accessor cat-min :initarg :min :initform 0 :type number
:documentation "Mininum value of category")
(cat-max
:accessor cat-max :initarg :max :initform 1 :type number
:documentation "Maximum value of category"))
(:documentation "A category"))
现在,我想使用这个类作为哈希表的键。实例的地址可以很容易地与eq 进行比较。但是问题是,这个category 类可能有多个相同的实例,我希望哈希表也能将其识别为键。
所以,我试图像这样覆盖make-hash-table 函数的:test 参数:
(make-hash-table :test #'(lambda (a b) (and (equal (cat-channel-name a) (cat-channel-name b))
(eq (cat-min a) (cat-min b))
(eq (cat-max a) (cat-max b)))
很遗憾,这是不允许的。 :test 需要是函数 eq、eql、equal 或 equalp 之一的指示符。
解决这个问题的一种方法是将category 类变成一个结构,但我需要它成为一个类。有什么办法可以解决吗?
【问题讨论】:
-
为什么需要它作为一个类?
-
您想使用 instances 作为键还是类本身?
标签: common-lisp hashtable equality clos