This extension provides a simple abstraction for defining abstract interfaces with separate implementations.
- (interface NAME DEFINITION ...)syntax
Declares NAME to be an interface that provides the definitions DEFINITION .... DEFINITION should be a value definition of the form
(define NAME [VALUE])
(define (NAME VAR ...) [BODY ...])
The values and bodies of the given definitions are optional and default to (void) (for value definitions) or a procedure that signals a runtime error (for procedure definitions). If a value/body is given, it provides a default for later implementations of this interface (see below).
- (implementation NAME DEFINITION ...)syntax
Defines an implementation of interface NAME. An implementation is a record structure holding the given definitions. Note that implementation returns a first-class object, in contrast to interface which is a declaration.
Each definition declared in the interface can be accessed by invoking the definition-name with an implementation as its sole argument.
(interface counter (define (counter-new)) (define (counter-inc c)) (define (counter-get c))) (define simple-counter (implementation counter (define (counter-new) 0) (define counter-inc add1) (define counter-get identity))) (let ((c1 ((counter-new simple-counter)))) (print ((counter-get simple-counter) c1)) ; ==> "0" (let ((c2 ((counter-inc simple-counter) c1))) (print ((counter-get simple-counter) c2)))) ; ==> "1" (define logged-counter (implementation counter (define (counter-inc c) (let ((c2 (add1 c))) (print "increasing counter " c " to " c2 "! is this cool or what?") c2))))
This code is in the public domain
- fixed bug related to implementation constructors exported from modules
- added missing dependency
- Initial release