chickadee » record-variants » define-record-variant

(define-record-variant name-spec variant-spec slot1 slot2 ...)syntax


name-spec := (variant-name original-name) | variant-name
variant-spec := (variant-type ...)
variant-type := unsafe | unchecked | inline

Defines alternate accessor procedures to the existing record original-name according to variant-spec. The accessors will be defined using variant-name, as if (define-record variant-name slot1 slot2 ...) had been invoked, but they will operate on records of type original-name.

Variant type may be one of:

  • inline, so procedure definitions use define-inline;
  • unchecked, so accessors do not check the record type;
  • unsafe, so accessors use ##sys#slot and ##sys#setslot instead of the safe block-ref! and block-set!

and any combination of variant-type is allowed in variant-spec.

A constructor, make-VARIANT-NAME, is defined to create a record of the original type. If you are defining a variant on an existing record, this is here essentially for completeness, as unsafe and unchecked don't have any effect on the constructor -- though inline will inline it.

Additionally, one new procedure over define-record is created:

(check-VARIANT-NAME x): Checks that x is of the corresponding record type and returns x if so; otherwise throws an error. When compiled in unsafe mode no check is performed, regardless of variant-type.

unsafe and unchecked accessors are dangerous and should only be used internally in a module. Only use these when you are absolutely sure the object is of the correct type; it is highly recommended to use (check-VARIANT-NAME x) or call an original accessor before using these, after which the correct type is guaranteed. (Assuming no side effects anywhere else!)

Note that (define-record-variant foo () x y) is equivalent to (define-record foo x y) except that a check-foo procedure will be generated.