Re: [Chicken-users] Question about COOPS, generic methods, and modules
On Wed, Sep 22, 2010 at 09:06:33PM -0400, Taylor Venable wrote: #;1 (use coops) #;2 (load 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) -- That kind of makes sense. I have no idea how coops works, but loading the file again is more or less the same as typing in every expression in the file. See this session: #;1 (use coops) #;2 (define-class foo) #;3 (define old-foo foo) #;4 (define-method (bar (x foo)) (print hi)) Note: implicitly defining generic-procedure: bar #;5 (bar (make foo)) hi #;6 (define-class foo) #;7 (bar (make foo)) Error: (bar) no method defined for given argument classes: (#coops standard-class `foo') #;8 (bar (make old-foo)) hi As you can see, defining a new class creates a completely new class object which in this case happens to be bound to the same identifier (foo), and happens to be structurally identical, but it is a completely new class. The bar method is defined only for the class I defined at the start, as you can see at prompt #8. Cheers, Peter -- http://sjamaan.ath.cx -- The process of preparing programs for a digital computer is especially attractive, not only because it can be economically and scientifically rewarding, but also because it can be an aesthetic experience much like composing poetry or music. -- Donald Knuth ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Question about COOPS, generic methods, and modules
From: Taylor Venable tay...@metasyntax.net 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: `define-method' is basically only working if you had a previous `define-generic' definition for that method. To allow extending previously existing generics and for convenience, `define-method' will do an implicit `define-generic' if the syntactic/import environment does not contain a toplevel binding for this method-name. Inside a module, the syntactic environment will only contain what has been imported. In your second example, the module will create a fresh generic, removing the one that previously existed, since `lols' has not been imported and is thus not visible. cheers, felix ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
[Chicken-users] Question about COOPS, generic methods, and modules
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/ ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users