On this page:
prop:  auto-equal+  hash
8.16.0.4

16 Structs🔗ℹ

 (require scramble/struct) package: scramble-lib

Property that automatically adds an implementation of the prop:equal+hash property. If the property value is #t, the automatically generated equality predicate and hash code functions use all of the struct type’s fields. If the property value is a list, then the equality predicate and hash code functions use only the fields with the indexes in the list.

In addition to the indicated fields, the hash code function also depends on a random seed and the name of the struct type. A new random seed is generated for each instantiation of the scramble/struct module.

If prop:auto-equal+hash is attached to a struct type that has a super struct type, then the super struct type must also have the prop:auto-equal+hash, and the new equality and hash code functions extend the super type’s functions. If the super struct type does not have the prop:auto-equal+hash property, an error is raised.

> (struct point (x y)
    #:property prop:auto-equal+hash #t)
> (equal-hash-code (point 1 2))

986857526104817982

> (equal? (point 1 2) (point 1 2))

#t

> (equal? (point 1 2) (point 0 0))

#f

In the following example, the equality and hash code functions of the point3 struct type use only the z field out of point3’s fields, disregarding the color field, but they also use both of point’s fields.

> (struct point3 point (z color)
    #:property prop:auto-equal+hash (list (struct-field-index z)))
> (equal? (point3 1 2 3 #f)
          (point3 1 2 3 'red))

#t

> (equal? (point3 0 0 3 'red)
          (point3 1 2 3 'red))

#f

> (equal? (equal-hash-code (point3 1 2 3 #f))
          (equal-hash-code (point3 1 2 3 'red)))

#t