Version: 4.1.3.9
Multimethods
(require (planet "main.ss" ("murphy" "multimethod.plt" 1 0))) |
Provides procedures with versatile dispatch on any number of their arguments.
Inheritance hierarchies for method signatures can be defined in an ad-hoc fashion or class and interface hierarchies from scheme/class can be used.
A small usage example:
(derive 'rock 'item) |
(derive 'paper 'item) |
(derive 'scissors 'item) |
(define-multi (beats? a b) :: (vector-immutable a b)) |
(define-method (beats? a b) :: #(item item) |
#f) |
(define-method (beats? a b) :: #(rock scissors) |
#t) |
(define-method (beats? a b) :: #(paper rock) |
#t) |
(define-method (beats? a b) :: #(scissors paper) |
#t) |
> (derived? 'rock 'item) | ||||
#t | ||||
> (derived? 'rock 'rock) | ||||
#t | ||||
> (derived? 'rock 'paper) | ||||
#f | ||||
> (descendants 'item) | ||||
#hash((rock . #t) (paper . #t) (scissors . #t)) | ||||
> (beats? 'rock 'paper) | ||||
#f | ||||
> (beats? 'rock 'scissors) | ||||
#t | ||||
> (beats? 'limestone 'scissors) | ||||
multimethod: no method for signature #(limestone scissors) | ||||
> (derive 'limestone 'rock) | ||||
> (beats? 'limestone 'scissors) | ||||
#t | ||||
| ||||
#t | ||||
> (beats? 'limestone 'paper) | ||||
#f | ||||
| ||||
#t | ||||
> (beats? 'marble 'scissors) | ||||
multimethod: no method for signature #(marble scissors) |