guile-user
[Top][All Lists]
Advanced

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

Re: GOOPS: Customizing class instantiation


From: Pat Lasswell
Subject: Re: GOOPS: Customizing class instantiation
Date: Tue, 26 Sep 2006 08:26:22 -0700

On 9/26/06, Ludovic Courtès <address@hidden> wrote:

  (define-method (make-instance (c <my-class>))
    (format (current-error-port) "make-instance (~a)~%" c)
    (next-method))

Try adding a '. initargs' after '(c <my-class>)'.

cheers
pat

On 9/26/06, Ludovic Courtès <address@hidden> wrote:
Hi,

I'm trying to "dynamically" create classes which I would then like to
instantiate.  The behavior I would like to achieve is that:

  guile> (define c (make-a-class 'some-name))
  guile> (define obj (make c))
  guile> obj
  #<<my-class> some-name deadbeef>

To that purpose, I have a metaclass named `<my-class>' and a procedure
`make-a-class' that simply returns instances of `<my-class>'.  Since
`compute-cpl' is customized for instances of `<my-class>', I can make
sure that instances of `<my-class>' have a proper CPL that includes
`<top>'.  Here's the code:

  (use-modules (oop goops))
  (read-set! keywords 'prefix)

  (define-class <my-class> (<class>)
    (the-slot :init-value #t))


  (define-method (compute-cpl (c <my-class>))
    (format (current-error-port) "CPL (~a)~%" c)
    (list c <top>))

  (define-method (make-instance (c <my-class>))
    (format (current-error-port) "make-instance (~a)~%" c)
    (next-method))


  (define (make-a-class name)
    (let ((c (make <my-class>)))
      (slot-set! c 'name name)
      c))

The issue is that when instantiating an instance of `<my-class>' (read
that twice ;-)), I'm getting the following error:

  ;; Create a class (instance of `<my-class>').
  guile> (define c (make-a-class 'paf))
  CPL (#<<my-class> ??? 301f8930>)
  guile> c
  #<<my-class> paf 301f8930>

  ;; Instantiate it.
  guile> (make c)
  make-instance (#<<my-class> paf 301f8930>)

  <unnamed port>: In _expression_ (let* (#) (format # "make-instance (~a)~%" ...) ...):
  <unnamed port>: No applicable method for #<<generic> initialize (10)> in call (initialize #<struct 301f8930:301f7320> ())
  ABORT: (goops-error)

This is quite unexpected since I would expect, at least, the
`initialize' class for instances of `<top>' to be applicable.  The other
surprising thing is that the instance to be initialized is still seen as
#<struct ...> instead of #<paf ...> or some such.

Any idea?

Thanks,
Ludovic.


_______________________________________________
Guile-user mailing list
address@hidden
http://lists.gnu.org/mailman/listinfo/guile-user


reply via email to

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