Skip to content
Snippets Groups Projects

Add applicable records

Merged David Thompson requested to merge applicable-structs into main
1 unresolved thread
Compare and
9 files
+ 275
27
Compare changes
  • Side-by-side
  • Inline
Files
9
+ 56
2
@@ -1819,8 +1819,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] @
@@ -1859,6 +1860,59 @@ appear more than once in the fields section.
@end deffn
@subsection Applicable structs
Hoot's record type system can be used to create new types that can be
applied as if they were regular procedures. These are known as
@emph{applicable structs}. The @code{(hoot applicable-structs)}
module provides the special @code{<applicable-struct>} record type; it
defines a single field that should contain a procedure. Any record
type that descends from the @code{<applicable-struct>} lineage (via
@code{#:parent}) becomes procedure-like.
As a contrived example, an incrementing counter type could be
implemented as an applicable struct:
@lisp
(define-record-type <counter>
#:parent <applicable-struct>
(%make-counter procedure count)
counter?
(count counter-count set-counter-count!))
(define (make-counter)
(define (next!)
(let ((x (1+ (counter-count counter))))
(set-counter-count! counter x)
x))
(define counter (%make-counter next! 0))
counter)
(define c (make-counter))
(counter? c) ;; => #t
(procedure? c) ;; => #t
(c) ;; => 1
(c) ;; => 2
@end lisp
Note that @code{<counter>} objects are recognized as both counters
@emph{and} procedures!
@defvr {Variable} <applicable-struct>
The record type descriptor for applicable structs.
@end defvr
@deffn {Procedure} applicable-struct? obj
Return @code{#t} if @var{obj} is an applicable struct.
@end deffn
@deffn {Procedure} applicable-struct-procedure obj
Return the procedure stored within @var{obj}.
@end deffn
@node Pattern matching
@section Pattern matching
Loading