The MIT/GNU Scheme object-hashing facility provides a mechanism for generating a unique hash number for an arbitrary object. This hash number, unlike an object’s address, is unchanged by garbage collection. The object-hashing facility is used in the generation of the written representation for many objects (see Custom Output), but it can be used for anything that needs a stable identifier for an arbitrary object.
All of these procedures accept an optional argument called
hasher which contains the object-integer associations. If
given, this argument must be an object hasher as constructed by
make-object-hasher (see below). If not given, a default hasher
hash-object associates an exact non-negative integer with
object and returns that integer. If
previously called with object as its argument, the integer
returned is the same as was returned by the previous call.
hash-object guarantees that distinct objects (in the sense of
eqv?) are associated with distinct integers.
unhash-object takes an exact non-negative integer k and
returns the object associated with that integer. If there is no
object associated with k, or if the object previously associated
with k has been reclaimed by the garbage collector, an error of
condition-type:bad-range-argument is signalled. In other
hash-object previously returned k for some
object, and that object has not been reclaimed, it is the value of the
An object that is passed to
hash-object as an argument is not
protected from being reclaimed by the garbage collector. If all other
references to that object are eliminated, the object will be
reclaimed. Subsequently calling
unhash-object with the hash
number of the (now reclaimed) object will signal an error.
(define x (cons 0 0)) ⇒ unspecified (hash-object x) ⇒ 77 (eqv? (hash-object x) (hash-object x)) ⇒ #t (define x 0) ⇒ unspecified (gc-flip) ;force a garbage collection (unhash-object 77) error→
This predicate is true iff object has an associated hash number.
This predicate is true iff k is the hash number associated with some object.
Finally, this procedure makes new object hashers: