From: Tobias C. Rittweiler <tcr@fr...> - 2009-01-11 01:10:08
I'd like to specify the types for the keys and values of a
hash-table. These constraints should be checked in (SETF GETHASH) (and
GETHASH.) The type information can be used for the test and sxhash
functions that are used---to avoid dispatching.
(make-hash-table :test #'equal
#'(lambda (x y) (equal (truly-the simple-string x)
(truly-the simple-string y)))
as test and the alike with SXHASH which would be transformed to
SXHASH-SIMPLE-STRING as hashing function.
At the moment, I could use SB-INT:DEFINE-HASH-TABLE-TEST to define my
own test and hash function which do the type checking. However,
a) the type check would be done several times within each GETHASH
and (SETF GETHASH).
b) DEFINE-HASH-TABLE-TEST's interface is "sucky" (quote), not
exported, and will perhaps vanish soon,
c) it'd be boilerplate code.
I think I could and would be willing to come up with an implementation
of this. I'd like to receive comments first, then insert an appropriate
ticket into the bug tracker.
On Sun, Jan 11, 2009 at 3:09 AM, Tobias C. Rittweiler <tcr@...> wrote:
> I'd like to specify the types for the keys and values of a
> hash-table. These constraints should be checked in (SETF GETHASH) (and
> GETHASH.) The type information can be used for the test and sxhash
> functions that are used---to avoid dispatching.
I have been thinking about something along these lines as well.
However, I have been thinking of along the lines of an SB-DICTIONARY
contrib, and a DEFDICTIONARY macro.
Something maybe like:
(defun ensure (x table)
(declare (string-dict table))
(or (getdict x table)
(setf (getdict x table) x)))
which would allow open coding GETDICT (or more likely parts of it,
like the SXHASH.)
Not terribly religious about this versus extending MAKE-HASH-TABLE.
From: Tobias C. Rittweiler <tcr@fr...> - 2009-01-11 17:04:30
"Nikodemus Siivola" <nikodemus@...> writes:
> Not terribly religious about this versus extending MAKE-HASH-TABLE.
The advantage on extending MAKE-HASH-TABLE is that I can use
#+sbcl :key-type #+sbcl 'foo
#+sbcl :value-type #+sbcl 'quux)
in portable Common Lisp code, and benefit from the type checking in my
implementation of choice, and also enhance clarity for readers of the
code on what types are involved.