Next: , Previous: , Up: iklib records defs   [Index]


6.19.1.8 The custom hash function

In a use of the syntax define-record-type, the definition clause:

(hash-function ?expr)

allows the specification of an expression ?expr which must evaluate to a custom hash function protocol for instances of the record–type; iklib records hash, for details on record’s hash functions.

If the record–type has a parent: the protocol function is applied to the hash function of the parent (or #f if the parent has no hash function) and the resulting value must be a function, which becomes the hash function of the record–type.

If the record–type has no parent: the protocol function is called as a thunk and the resulting value must be a function, which becomes the hash function of the record–type.

As example using the typed language, the following record–type has no parent:

(define-record-type duo
  (fields one two)
  (hash-function
    (lambda ()
      (lambda ({O duo})
        (fx+ (fixnum-hash (.one O))
             (fixnum-hash (.two O)))))))

(hash (new duo 1 2))    ⇒ 3

the following record–type has a parent defining a hash function:

(define-record-type alpha
  (fields a)
  (hash-function
    (lambda ()
      (lambda ({O alpha})
        (.a O)))))

(define-record-type beta
  (fields b)
  (hash-function
    (lambda (alpha-hash)
      (lambda ({O beta})
        (fx+ (alpha-hash O) (.b O))))))

(hash (new beta 1 2))) ⇒ 3

The hash syntax can always compute a hash value for records; when the record–type has no custom has function, the core primitive record-hash is used record-hash.