You are looking at historical revision 36282 of this page. It may differ significantly from its current revision.

interfaces

This extension provides a simple abstraction for defining abstract interfaces with separate implementations.

Documentation

interface

[syntax] (interface NAME DEFINITION ...)

Declares NAME to be an interface that provides the definitions DEFINITION .... DEFINITION should be a value definition of the form

 (define NAME [VALUE])

or

 (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

[syntax] (implementation NAME DEFINITION ...)

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.

Example

(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))))

Requirements

records

Author

felix winkelmann

License

This code is in the public domain

Version History

0.4
ported to CHICKEN 5
0.3
fixed bug related to implementation constructors exported from modules
0.2
added missing dependency
0.1
Initial release