This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

GOOPS: inherit method


Hi,

How can I inherit a method?  It works with change-class on a clone of
the original object, but I would prefer a way to call the method
without allocating space for a copy of the object.

Example:
------------------------------------------------------------
(use-modules (oop goops))

(define-class <1D> ()     (x))
(define-class <2D> (<1D>) (y))

(define punkt (make <2D>))

punkt       => #<<2D> 40218528>

(slot-set! punkt 'x 3.14)
(slot-set! punkt 'y 2.71)

(define-method str ((obj <1D>))
  (number->string (slot-ref obj 'x)))

(str punkt) => "3.14"

(define-method str ((obj <2D>))
  (string-append "{"
                 (str (change-class (shallow-clone obj) <1D>))
                 ";"
                 (number->string (slot-ref obj 'y))
                 "}"))

(str punkt) => "{3.14;2.71}"
punkt       => #<<2D> 40218528>
------------------------------------------------------------

Sascha

-- 
Freie Software ist der Ausgang des Menschen aus seiner
selbstverschuldeten Unmündigkeit.                (Immanuel Kant, 1784)
                   http://www.fordham.edu/halsall/mod/kant-whatis.html

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]