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: bug or strange feature in slot initialization


I am not completely sure this is a bug or a feature, but it is at
least very confusing.

If a slot in a class is a list, and if that slot is initialized in the
the initialize method, then all of the instances of this class will
refer to the same list: if any of these instances modify the contents
of the list, it will be seen in all the other objects, including those
which will be created later.  Note that this is the same if the object
is initialized with a #:init-value keyword in the class declaration
(which could be considered as a feature since the class declaration
block is evaluated only once).

Here is the complete sample code:

  (use-modules (oop goops))
  
  ;; Define the class and its initialize method
  (define-class <foo> ()
    (bar #:accessor bar))
  
  (define-method initialize ((x <foo>) args)
    (next-method)
    (set! (bar x) '(42)))
  
  ;; Create two instances of <foo>
  (let ((a (make <foo>))
        (b (make <foo>)))
  
    ;; Modify one of these instances
    (set-cdr! (bar a) '(43))
  
    ;; Alas, the other instance has been modified as well!
    (display (bar b))(newline) ;; Prints (42 43)
  
    ;; Even newly-created objects will have the wrong initialization
    (display (bar (make <foo>)))(newline) ;; Prints (42 43) 
    
    )

My Goops is 0.1.6, and my Guile is 1.3.4.

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