使用 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=))
查看 Common Lisp Hyperspec,看来我只能使用 eq、eql、equal 或 equalp 来测试相等性。
我有什么办法可以做到这一点吗?或者这只是一件非常愚蠢的事情,这就是标准不允许这样做的原因?
I'd like to use Common Lisp CLOS objects as keys in a hashtable. I thought it would be as simple as this:
(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=))
Checking out the Common Lisp Hyperspec, it seems I can only use eq, eql, equal, or equalp to test equality.
Is there any way I can do this? Or is this just a really stoopid thing to do, and that's why the standard doesn't allow it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Common Lisp 标准没有提供任何机制来为哈希表提供额外的测试函数(超出标准)。您有 2 个选择:
ext:define-hash-表测试
(文档):test
参数的非标准值,并具有:hash-function
参数 (Allegro,Lispworks)。Common Lisp standard does not provide any mechanism to provide additional test functions for hash-tables (beyond standard ones). You have 2 options:
sb-ext:define-hash-table-test
function (documentation)ext:define-hash-table-test
(documentation):test
argument and has:hash-function
argument (Allegro, Lispworks).