Using Common Lisp CLOS objects as keys in a hashtable?
Asked Answered
L

1

9

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?

Luxembourg answered 7/7, 2011 at 2:5 Comment(0)
U
9

Common Lisp standard does not provide any mechanism to provide additional test functions for hash-tables (beyond standard ones). You have 2 options:

  1. Use genhash genhash which is portable hash-table implementation (not compatible with built-in ones)
  2. Use non-standard extensions:
  3. SBCL has sb-ext:define-hash-table-test function (documentation)
  4. Clisp has a similar function ext:define-hash-table-test (documentation)
  5. Allegro and Lispworks accept non-standard values for :test argument and has :hash-function argument (Allegro, Lispworks).
Uranology answered 7/7, 2011 at 3:15 Comment(1)
Genhash is, if nothing else, pretty stable (and goverened by a CDR).Treehopper

© 2022 - 2024 — McMap. All rights reserved.