Generic functions

With the STK object layer, execution of a method doesn't use the classical message sending mechanism as in numerous object languages but generic functions. The mechanism implemented in STK is a subset of the generic functions of CLOS. As in CLOS, a generic function can have several methods associated with it. These methods describe the generic function behaviour according to the type of its parameters. A method for a generic function is defined with the define-method form.

Following example shows three methods of the generic function value-of:

   (define-method value-of ((obj <Scale>))
     (string->number ((Id obj) 'get)))

   (define-method value-of ((obj <Entry>))
     ((Id obj) 'get))
    
   (define-method value-of (obj)
     (error "Bad call: " obj))
When calling the value-of generic function, system will choose the more adequate method, according to its parameter type: if parameter is a scale or an entry, first or second method is called; otherwise the third method is called since it doesn't discriminates in favour of a particular parameter type.

Setter method are a special kind of methods which are used with the generalized set!. Here are the corresponding setter methods to previous value-of:

    (define-method (setter value-of)
                   ((obj <Scale>) value)
      ((Id obj) 'set value))

    (define-method (setter value-of)
                   ((obj <Entry>) value)
      ((Id obj) 'delete 0 'end)
      ((Id obj) 'insert 0 value))
    
    (define-method (setter value-of) (obj)
      (error "Bad call: " obj))
One of these methods will be called when evaluating following form, depending of type of x:
    (set! (value-of x) 100)
As we can see here, generic functions yield things more homogeneous than what we can have at STK first level. Indeed getting and setting the value of an entry or a scale can now be done in a similar fashion with those methods.