Skip to content
Snippets Groups Projects

Add applicable records

Merged David Thompson requested to merge applicable-structs into main
Compare and Show latest version
2 files
+ 16
14
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 62
2
@@ -1826,8 +1826,9 @@ and @code{hashv} is used by @code{make-eqv-hashtable}.
@node Records
@section Records
Hoot extends the R7RS @code{define-record-type} form with additional
features such as inheritance and opaque types.
The @code{(hoot records)} module extends the R7RS
@code{define-record-type} form with additional features such as
inheritance and opaque types.
@deffn {Syntax} define-record-type name @
[#:printer] [#:parent] [#:uid] [#:extensible? #t] [#:opaque? #f] @
@@ -1866,6 +1867,65 @@ appear more than once in the fields section.
@end deffn
@c @subsection Applicable records
@c Hoot's record type system can be used to create new types that can be
@c applied as if they were regular procedures. These are known as
@c @emph{applicable records}. The @code{<applicable-reord>} type defines
@c a single field that should contain a procedure. Any record type that
@c descends from the @code{<applicable-record>} lineage (via
@c @code{#:parent}) becomes procedure-like.
@c As a contrived example, an incrementing counter could be implemented
@c like this:
@c @lisp
@c (use-modules (hoot records))
@c (define-record-type <counter>
@c #:parent <applicable-record>
@c (%make-counter procedure count)
@c counter?
@c (count counter-count set-counter-count!))
@c (define (make-counter)
@c (define (next!)
@c (let ((x (1+ (counter-count counter))))
@c (set-counter-count! counter x)
@c x))
@c (define counter (%make-counter next! 0))
@c counter)
@c (define c (make-counter))
@c (counter? c) ;; => #t
@c (procedure? c) ;; => #t
@c (c) ;; => 1
@c (c) ;; => 2
@c @end lisp
@c Note that @code{<counter>} objects are recognized as both counters
@c @emph{and} procedures!
@c @defvr {Variable} <applicable-record>
@c The record type descriptor for applicable records.
@c @end defvr
@c @deffn {Procedure} applicable-record? obj
@c Return @code{#t} if @var{obj} is an applicable record.
@c @end deffn
@c @deffn {Procedure} applicable-record-procedure obj
@c Return the procedure stored within the applicable record @var{obj}.
@c @end deffn
@c @deffn {Procedure} set-applicable-record-procedure! obj proc
@c Set the procedure stored within the applicable record @var{obj} to
@c @var{proc}.
@c @end deffn
@node Pattern matching
@section Pattern matching
Loading