Version: 4.1.3.9

Multimethods

    1 Ad-Hoc Hierarchies of Values

    2 Multimethods

      2.1 Procedural Protocol

      2.2 Wrapper Protocol

      2.3 Syntactic Sugar

    3 License

 (require (planet "main.ss" ("murphy" "multimethod.plt" 1 1)))

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

  > (parameterize-multi (beats?)
      (define-method (beats? a b) :: #(limestone paper)
        #t)
      (beats? 'limestone 'paper))

  #t

  > (beats? 'limestone 'paper)

  #f

  > (parameterize ([global-hierarchy (derive (global-hierarchy)
                                             'marble 'rock)])
      (beats? 'marble 'scissors))

  #t

  > (beats? 'marble 'scissors)

  multimethod: no method for signature #(marble scissors)