chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Question about COOPS, generic methods, and modules


From: Taylor Venable
Subject: [Chicken-users] Question about COOPS, generic methods, and modules
Date: Wed, 22 Sep 2010 21:06:33 -0400

Hi there!

I found this while writing some code for work earlier (I'm lucky enough to be able to write testing programs in the language of my choice). I define a class and a generic method in a module in a file, then load it into the REPL. I can make an instance of that class, binding it to a variable at the toplevel, and use the generic method just fine. If I then reload my code, I cannot use the generic method with the bound instance: I get the "no method defined for given argument classes" error. If my class and generic method definitions are not in a module, everything works fine. I'm wondering if this is me misunderstanding something, or it is intentional behaviour, or maybe it's a bug. Here is a concrete example:

;; FILE: coops-test-1.scm

(define-class <a-class> (<standard-object>)
  ((a 2) (b 3)))

(define-method (lols (instance <a-class>))
  (+ (slot-value instance 'a) (slot-value instance 'b)))

;; FILE: coops-test-2.scm

(module foo
  (<a-class>
   lols)

  (import scheme chicken coops)

  (define-class <a-class> (<standard-object>)
    ((a 2) (b 3)))

  (define-method (lols (instance <a-class>))
    (+ (slot-value instance 'a) (slot-value instance 'b)))
)

;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#;1> (use coops)
; loading /opt/chicken/lib/chicken/5/coops.import.so ...
; loading /opt/chicken/lib/chicken/5/scheme.import.so ...
; loading /opt/chicken/lib/chicken/5/chicken.import.so ...
; loading /opt/chicken/lib/chicken/5/matchable.import.so ...
; loading /opt/chicken/lib/chicken/5/srfi-1.import.so ...
; loading /opt/chicken/lib/chicken/5/data-structures.import.so ...
; loading /opt/chicken/lib/chicken/5/extras.import.so ...
; loading /opt/chicken/lib/chicken/5/record-variants.import.so ...
; loading /opt/chicken/lib/chicken/5/foreign.import.so ...
; loading /opt/chicken/lib/chicken/5/coops.so ...
#;2> (load "coops-test-1.scm")
; loading coops-test-1.scm ...

Note: implicitly defining generic-procedure: lols
#;3> (define instance (make <a-class>))
#;4> (lols instance)
5
#;5> (load "coops-test-1.scm")
; loading coops-test-1.scm ...
#;6> (lols instance)
5

;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#;1> (use coops)
; loading /opt/chicken/lib/chicken/5/coops.import.so ...
; loading /opt/chicken/lib/chicken/5/scheme.import.so ...
; loading /opt/chicken/lib/chicken/5/chicken.import.so ...
; loading /opt/chicken/lib/chicken/5/matchable.import.so ...
; loading /opt/chicken/lib/chicken/5/srfi-1.import.so ...
; loading /opt/chicken/lib/chicken/5/data-structures.import.so ...
; loading /opt/chicken/lib/chicken/5/extras.import.so ...
; loading /opt/chicken/lib/chicken/5/record-variants.import.so ...
; loading /opt/chicken/lib/chicken/5/foreign.import.so ...
; loading /opt/chicken/lib/chicken/5/coops.so ...
#;2> (load "coops-test-2.scm")
; loading coops-test-2.scm ...

Note: implicitly defining generic-procedure: lols
#;3> (import foo)
#;4> (define instance (make <a-class>))
#;5> (lols instance)
5
#;6> (load "coops-test-2.scm")
; loading coops-test-2.scm ...

Note: implicitly defining generic-procedure: lols
#;7> (lols instance)

Error: (lols) no method defined for given argument classes: (#<coops standard-class `<a-class>'>)

        Call history:

        <syntax>          (lols instance)
        <eval>    (lols instance)       <--

I'm using Chicken 4.6.1 (git 7ac10a2fb9b04d114af97d8c9918bffae38cc534) on Linux AMD64 with COOPS 0.8 to test this. I notice that you get the warning about automatically defining the generic method twice, at each load, maybe that's indicative of the problem? Is there a way to make this work? Thanks.

--
Taylor C. Venable
http://metasyntax.net/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]