使用Common Lisp CLOS对象作为哈希表中的键? [英] Using Common Lisp CLOS objects as keys in a hashtable?

查看:94
本文介绍了使用Common Lisp CLOS对象作为哈希表中的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Common Lisp CLOS对象作为哈希表中的键.我以为就这么简单:

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=))

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

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?

推荐答案

Common Lisp标准不提供任何机制来提供其他测试功能(超越标准的功能).您有2个选择:

Common Lisp standard does not provide any mechanism to provide additional test functions (beyound standard ones). You have 2 options:

  1. 使用genhash genhash 是可移植的哈希表实现(与内置的哈希表不兼容)
  2. 使用非标准扩展名:
  1. Use genhash genhash which is portable hash-table implementation (not compatible with built-in ones)
  2. Use non-standard extensions:
  1. SBCL具有sb-ext:define-hash-table-test功能(文档)
  2. Clisp具有类似的功能ext:define-hash-table-test(文档)
  3. Allegro ans Lispworks接受:test参数的非标准值,并具有:hash-function参数(
  1. SBCL has sb-ext:define-hash-table-test function (documentation)
  2. Clisp has a similar function ext:define-hash-table-test (documentation)
  3. Allegro ans Lispworks accept non-standard values for :test argument and has :hash-function argument (Allegro, Lispworks).

这篇关于使用Common Lisp CLOS对象作为哈希表中的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆