Next: , Previous: , Up: 2015   [Contents][Index]

Super-type constructor protocols for records

Posted on September 12, 2015

Vicare’s library (vicare) now exports a new keyword syntactic binding super-protocol, as clause for define-record-type. All the changes discussed here are in the master branch.

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

(super-protocol ?expr)

allows the specification of a constructor protocol to be used by sub–types of a record–type. Usually a sub–type make use of the protocol function built from the constructor protocol specified by the clause protocol, but the clause super-protocol overrides it.

The super-protocol works like protocol, its ?expr argument must be an expression evaluating to a protocol function; the protocol function accepts as single argument the constructor function of its super–type and it must return the record instance.

Here is an example in which the record–type alpha is the super–type of the record–type beta:

(define-record-type alpha
  (fields a b)
    (lambda (make-record)
      (lambda (a b)
        (make-record (+ 10 a) (+ 20 b))))))

(define-record-type beta
  (parent alpha)
  (fields c d))

(let ((R (make-alpha 1 2)))
  (values (alpha-a R)
          (alpha-b R)))
⇒ 1 2

(let ((R (make-beta 1 2 3 4)))
  (values (alpha-a R)
          (alpha-b R)
          (beta-c  R)
          (beta-d  R)))
⇒ 11 22 3 4

the default constructor function make-alpha builds instances of alpha with the default constructor; the constructor of beta receives as constructor function the result of calling the super-protocol of alpha.

Next: , Previous: , Up: 2015   [Contents][Index]