[plt-scheme] 202.2
The exp-tagged code for MzScheme and MrEd in CVS is now version 202.2.
202.2 supports structure types whose instances act as procedures.
There is no high-level syntax for procedureness, currently. Use the
primitive `make-struct-type' function to declare procedure information
for a new structure type.
Procedureness can be added to a structure in either of two ways:
1. A field within the structure is designated as containing a
procedure. This is useful for wrapping procedures with information.
Example (from the documentation):
(define-values (struct:ap make-annotated-proc
annotated-proc? ap-ref ap-set!)
(make-struct-type 'anotated-proc #f 2 0 #f null #f 0))
;; field 0 is the procedure-container--------------^
(define (proc-annotation p) (ap-ref p 1))
(define plus1 (make-annotated-proc
(lambda (x) (+ x 1))
"adds 1 to its argument"))
(procedure? plus1) ; => #t
(annotated-proc? plus1) ; => #t
(plus1 10) ; => 11
(proc-annotation plus1) ; => "adds 1 to its argument"
2. A method-like procedure can be attached to the structure type,
instead of individual instances. This is useful for making
applications of instances act like a particular function on the
instances (which is what Lauri Alanko wants).
Example (from the documentation):
(define-values (struct:fish make-fish fish? fish-ref fish-set!)
(make-struct-type 'fish #f 2 0 #f null #f
;; Here's the procedure for app. of an instance:
(lambda (f n) (fish-set! f 0 (+ n (fish-ref f 0))))))
(define (fish-weight f) (fish-ref f 0))
(define (fish-color f) (fish-ref f 1))
(define wanda (make-fish 12 'red))
(fish? wanda) ; => #t
(procedure? wanda) ; => #t
(fish-weight wanda) ; => 12
(for-each wanda '(1 2 3))
(fish-weight wanda) ; => 18
Matthew