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


6.19.1.4 The custom predicate

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

(type-predicate ?expr)

allows the specification of an expression ?expr which, applied to the default type predicate, must evaluate to a custom predicate function for instances of the record–type.

When the clause type-predicate is not used, the syntax define-record-type just defines the default record–type predicate:

(define-record-type duo
  (fields one two))

(duo? (make-duo 1 2))   ⇒ #t
(duo? (make-warning))   ⇒ #f
(duo? 123)              ⇒ #f

When the clause type-predicate is used, the syntax define-record-type binds to the type predicate’s syntactic identifier the result of applying ?expr to the default type predicate. In the following example the custom predicate is just the default predicate itself:

(define-record-type duo
  (fields one two)
  (type-predicate
    (lambda (duo?)
      duo?)))

(duo? (make-duo 1 2))   ⇒ #t
(duo? (make-warning))   ⇒ #f
(duo? 123)              ⇒ #f

in the following example the custom predicate accepts records wrapped into lists and vectors:

(define-record-type duo
  (fields one two)
  (type-predicate
    (lambda (duo?)
      (lambda (obj)
        (or (duo? obj)
            (and (list? obj)
                 (duo?  (car obj))
                 (null? (cdr obj)))
            (and (vector? obj)
                 (= 1 (vector-length obj))
                 (duo? (vector-ref obj 0))))))))

(define O
  (make-duo 1 2))

(duo? O)                ⇒ #t
(duo? (list O))         ⇒ #t
(duo? (vector O))       ⇒ #t
(duo? (make-warning))   ⇒ #f
(duo? 123)              ⇒ #f