Next: , Up: methods concrete   [Contents][Index]


7.1.1 Overloaded methods

The clause method allows the definition of overloaded functions, with multiple function specialisations associated to the same method name. Example:

(define-record-type <alpha>
  (fields a b)

  (method ({doit <list>} {A <fixnum>})
    (list (.a this) (.b this) 'fixnum A))

  (method ({doit <list>} {A <symbol>})
    (list (.a this) (.b this) 'symbol A))

  (method ({doit <list>} {A <number>} {B <number>})
    (list (.a this) (.b this) 'numbers A B)))

(define O
  (new <alpha> 1 2))

(.doit O 123)           ⇒ (1 2 fixnum 123)
(.doit O 'ciao)         ⇒ (1 2 symbol ciao)
(.doit O 3 4)           ⇒ (1 2 numbers 3 4)

we can think of the methods defined above as expanding to the following definitions:

(define/overload ({doit <list>} {subject <alpha>}
                      {A <fixnum>})
  (fluid-let-syntax ((this (make-synonym-transformer #'subject)))
    (list (.a this) (.b this) 'fixnum A)))

(define/overload ({doit <list>} {subject <alpha>}
                      {A <symbol>})
  (fluid-let-syntax ((this (make-synonym-transformer #'subject)))
    (list (.a this) (.b this) 'symbol A)))

(define/overload ({doit <list>} {subject <alpha>}
                      {A <number>} {B <number>})
  (fluid-let-syntax ((this (make-synonym-transformer #'subject)))
    (list (.a this) (.b this) 'numbers A B)))

where subject is a non–accessible identifier.