Search code examples
oopschemechicken-scheme

What is the purpose of define-generic in coops object system in Chicken Scheme


I'm trying to figure out what is the purpose of define-generic in goops library (http://wiki.call-cc.org/eggref/4/coops), it's similar to CLOS if it can help someone (I don't know CLOS either).

I can define a class and a method this way:

(define-class <complex> () (x y))

(define-method (sum (a <complex>) (b <complex>))
  (make <complex>
    'x (+ (slot-value a 'x) (slot-value b 'x))
    'y (+ (slot-value a 'y) (slot-value b 'y))))

But what is the purpose of this?

(define-generic (sum <complex> <complex>))

From the docs:

[syntax] (define-generic (NAME ARGUMENT ...))

Defines a generic procedure, a procedure specialized for one or more argument types. ARGUMENT ... defines the number of specialized arguments this generic procedure shoud use to dispatch to the correct method. The generic procedure may receive additional arguments, but those will not be used to determine the method.

Could someone give me a simple example explaining the differences beetween define-method and define-generic?


Solution

  • A generic procedure is a special kind of object that consists of (one or) several specialised methods.

    define-generic defines such an object, and define-method adds methods to it, creating it if it doesn't exist.