使用Common Lisp CLOS对象作为哈希表中的键?

And*_*osh 9 hashtable key common-lisp clos

我想使用Common Lisp CLOS对象作为哈希表中的键.我觉得它会像这样简单:

(defclass my-class () ((a :accessor a :initarg a)))

(defun my-class= (my-instance-1 my-instance-2)
(equal (a my-instance-1) (a my-instance-2)))

(defparameter my-hash-table (make-hash-table :test #'my-class=))
Run Code Online (Sandbox Code Playgroud)

检查Common Lisp Hyperspec,似乎我只能使用eq,eql,equal或equalp来测试相等性.

有什么方法可以做到这一点吗?或者这只是一个真正的stoopid事情,这就是为什么标准不允许它?

dmi*_*_vk 9

Common Lisp标准没有提供任何提供额外测试功能的机制(beyound标准功能).你有2个选择:

  1. 使用genhash genhash,它是可移植的哈希表实现(与内置的不兼容)
  2. 使用非标准扩展:
    1. SBCL有sb-ext:define-hash-table-test功能(文档)
    2. Clisp有类似的功能ext:define-hash-table-test(文档)
    3. Allegro ans Lispworks接受:test参数的非标准值并具有:hash-function参数(Allegro,Lispworks).