Skip to content
Snippets Groups Projects

Add applicable records

Merged David Thompson requested to merge applicable-structs into main
1 unresolved thread
9 files
+ 226
12
Compare changes
  • Side-by-side
  • Inline
Files
9
+ 56
2
@@ -1819,8 +1819,9 @@ and @code{hashv} is used by @code{make-eqv-hashtable}.
@@ -1819,8 +1819,9 @@ and @code{hashv} is used by @code{make-eqv-hashtable}.
@node Records
@node Records
@section Records
@section Records
Hoot extends the R7RS @code{define-record-type} form with additional
The @code{(hoot records)} module extends the R7RS
features such as inheritance and opaque types.
@code{define-record-type} form with additional features such as
 
inheritance and opaque types.
@deffn {Syntax} define-record-type name @
@deffn {Syntax} define-record-type name @
[#:printer] [#:parent] [#:uid] [#:extensible? #t] [#:opaque? #f] @
[#:printer] [#:parent] [#:uid] [#:extensible? #t] [#:opaque? #f] @
@@ -1859,6 +1860,59 @@ appear more than once in the fields section.
@@ -1859,6 +1860,59 @@ appear more than once in the fields section.
@end deffn
@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
@node Pattern matching
@section Pattern matching
@section Pattern matching
Loading