1. record-variants
    1. Interface
      1. define-record-variant
      2. define-record-type-variant
    2. Examples
    3. Author
    4. Version history
    5. Acknowledgements
    6. License

record-variants

record-variants defines optimized variants on existing records defined with define-record, and on SRFI 9 records defined with define-record-type. You may also create new record types if desired.

Interface

define-record-variant

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

where:

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:

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.

define-record-type-variant

[syntax] (define-record-type-variant name-spec variant-spec pred-spec constructor field-spec)

where:

name-spec := (variant-name original-name) | variant-name
variant-spec := (variant-type ...)
variant-type := unsafe | unchecked | inline
pred-spec := (predicate checker) | (predicate) | predicate
constructor, field-spec: as in SRFI 9

Defines alternate accessor procedures to the existing SRFI 9 record-type original-name according to variant-spec.

name-spec acts as it does in define-record-variant, including constructor generation behavior.

pred-spec may be a predicate identifier or a list containing a predicate identifier and optionally a "checker" identifier. The checker identifier is used as the name of the generated check-VARIANT-NAME procedure, which again behaves as in define-record-variant. If the checker identifier is omitted, no check procedure is generated.

See define-record-variant and SRFI 9 for further details.

Examples

Create a variant on an existing record node and a new record type %lru-cache. Use e.g. (%lru-cache lru-cache) as name-spec to create a variant on the existing record lru-cache.

(define-record node prev next key value)
(define-record-variant (%node node)
  (unsafe unchecked inline)
  prev next key value)

(define-record-type-variant %lru-cache
  (unsafe unchecked inline)
  (%make-lru-cache ht head tail)
  (%lru-cache? %check-lru-cache)     
  (ht %lru-cache-ht)
  (head %lru-cache-head %lru-cache-head-set!)     
  (tail %lru-cache-tail %lru-cache-tail-set!))

(define-inline (lookup c k)
  (hash-table-ref/default (%lru-cache-ht (%check-lru-cache c)) k #f))

(define (lru-cache-ref c k)
  (and-let* ((n (lookup c k)))   ; c now guaranteed to be valid
    (check-%node n)              ; n now guaranteed to be valid
    (if (not (%node-prev n))     ; MRU
        (%node-value n)
        (let ((nx (%node-next n))
              (pr (%node-prev n)))
          (when pr
            (check-%node pr)     ; pr now valid
            (%node-next-set! pr nx)
            (%node-prev-set! n #f)
            (when (eq? n (%lru-cache-tail c))
              (%lru-cache-tail-set! c pr)))
          (when nx
            (check-%node nx)     ; nx now valid
            (%node-prev-set! nx pr))
          (let ((head (%lru-cache-head c)))
            (check-%node head)   ; head now valid
            (%node-prev-set! head n)
            (%node-next-set! n head)
            (%lru-cache-head-set! c n)
            (%node-value n)) ))))

Author

Jim Ursetto

Version history

Acknowledgements

Inspiration was taken from Kon Lovett's misc-extn-record.

License

Copyright (c) 2009 Jim Ursetto.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

 Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
 Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
 Neither the name of the author nor the names of its contributors 
  may be used to endorse or promote products derived from this software 
  without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.