Generic Predicate Dispatch

Implements support for "generic operators" - procedures which can dispatch based on arbitrary argument predicates. Can be used to implement single, multiple and more complex types of dispatch. Internally, generic operators use a decision tree so that for a given argument, the associated predicate is executed at most once.

Procedures

genop? obj

defhandler operator handler {argument-predicates}

Adds a new handler for a generic operator. See defgen for an example.

any? x

A predicate of one argument which always returns true. Provided for convenience.


Macros

defgen name num-arguments [optional: default-handler]

Defines a new generic operator. For example:

(defgen my-add 2 (lambda x (error "Sorry, I don't know what to do")))
(defhandler my-add (lambda (a b) (+ a b)) number? number?)
(defhandler my-add (lambda (a b) (string-append a b)) string? string?)

(my-add 4 5)
;Value: 9

(my-add "hello, " "world!")
;Value 25: "hello, world!"

(my-add 'a 'b)

;Sorry, I don't know what to do
;To continue, call RESTART with an option number:
 (RESTART 1) => Return to read-eval-print level 1.


Scheme Power Tools Documentation
(c) Maciej Pacula 2010-2011
http://mpacula.com