unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* GOOPS: Customizing class instantiation
@ 2006-09-26  8:24 Ludovic Courtès
  2006-09-26 11:47 ` Ludovic Courtès
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Ludovic Courtès @ 2006-09-26  8:24 UTC (permalink / raw)


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
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: GOOPS: Customizing class instantiation
  2006-09-26  8:24 GOOPS: Customizing class instantiation Ludovic Courtès
@ 2006-09-26 11:47 ` Ludovic Courtès
  2006-09-26 15:26 ` Pat Lasswell
  2006-09-27 13:16 ` Ludovic Courtès
  2 siblings, 0 replies; 6+ messages in thread
From: Ludovic Courtès @ 2006-09-26 11:47 UTC (permalink / raw)


Hi,

ludovic.courtes@laas.fr (Ludovic Courtès) writes:

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

This should rather be: #<some-name 0123abcd>.

> This is quite unexpected since I would expect, at least, the
> `initialize' class for instances of `<top>' to be applicable.

And this should read: "the `initialize' _method_"...

Ludovic.


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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: GOOPS: Customizing class instantiation
  2006-09-26  8:24 GOOPS: Customizing class instantiation Ludovic Courtès
  2006-09-26 11:47 ` Ludovic Courtès
@ 2006-09-26 15:26 ` Pat Lasswell
  2006-09-27  8:20   ` Ludovic Courtès
  2006-09-27 13:16 ` Ludovic Courtès
  2 siblings, 1 reply; 6+ messages in thread
From: Pat Lasswell @ 2006-09-26 15:26 UTC (permalink / raw)



[-- Attachment #1.1: Type: text/plain, Size: 2605 bytes --]

On 9/26/06, Ludovic Courtès <ludovic.courtes@laas.fr> 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 <ludovic.courtes@laas.fr> 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
> Guile-user@gnu.org
> http://lists.gnu.org/mailman/listinfo/guile-user
>

[-- Attachment #1.2: Type: text/html, Size: 3883 bytes --]

[-- Attachment #2: Type: text/plain, Size: 140 bytes --]

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: GOOPS: Customizing class instantiation
  2006-09-26 15:26 ` Pat Lasswell
@ 2006-09-27  8:20   ` Ludovic Courtès
  0 siblings, 0 replies; 6+ messages in thread
From: Ludovic Courtès @ 2006-09-27  8:20 UTC (permalink / raw)
  Cc: Guile-User

Hi,

"Pat Lasswell" <imofftoseethewizard@gmail.com> writes:

> On 9/26/06, Ludovic Courtès <ludovic.courtes@laas.fr> 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>)'.

I shouldn't be needed and it makes no difference.  The
no-applicable-method error occurs when invoking `initialize' on the
newly allocated struct.

Thanks,
Ludovic.


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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: GOOPS: Customizing class instantiation
  2006-09-26  8:24 GOOPS: Customizing class instantiation Ludovic Courtès
  2006-09-26 11:47 ` Ludovic Courtès
  2006-09-26 15:26 ` Pat Lasswell
@ 2006-09-27 13:16 ` Ludovic Courtès
  2006-09-28  8:50   ` Neil Jerram
  2 siblings, 1 reply; 6+ messages in thread
From: Ludovic Courtès @ 2006-09-27 13:16 UTC (permalink / raw)


Hi,

Nobody won the prize, because nobody gave the correct answer.  For those
of you who played, I'm giving the correct answer below.  ;-)

ludovic.courtes@laas.fr (Ludovic Courtès) writes:

>   (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)

The whole issue here is that `%allocate-instance'
(aka. `scm_sys_allocate_instance') returns an apparently improperly
initialized object (a raw struct) instead of a regular GOOPS object that
has a class, etc.  Since there are no `initialize' methods for raw
structs, we get this no-applicable-method error.

But why is it so?  Looking at `goops.c', it's actually `wrap_init ()'
that returns the offending struct.  However, `wrap_init ()' _does_
specify class information as part of the type tag.  So perhaps it is the
class information that is not properly initialized in the case of an
instance of `<my-class>'?

The correct way to instantiate a class is found in `make-class' in
`goops.scm'.  This function passes several keyword arguments to `make'
that my `make-a-class' function did not provide.  In particular, the
`:dsupers' argument is the one that must not be forgotten:

  guile> (make (make <my-class>))
  [...]
  <unnamed port>: No applicable method for #<<generic> initialize (11)> in call (initialize #<struct 104ac060:104ae270> ())

  guile> (make (make <my-class> :dsupers (list <top>)))
  #<??? 104321b0>

Note that the `compute-cpl' method specialized for `<my-class>' is still
needed and it must contain a class for which an `initialize' method
exists (i.e., not `<top>'):

  (define-method (compute-cpl (c <my-class>))
    (list c <object> <top>))

Notice: `compute-cpl' and `:dsupers' don't even have to agree...

Now, this does not really explain why an instance of such an
improperly-initialized class would show up as a raw struct.  Well,
clarifying is left as an exercise to the reader.  :-)


In short, the lesson is: one should use `make-class' (although
it's undocumented) rather than `(make <class> ...)' when instantiating a
new class.

Thanks,
Ludovic.


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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: GOOPS: Customizing class instantiation
  2006-09-27 13:16 ` Ludovic Courtès
@ 2006-09-28  8:50   ` Neil Jerram
  0 siblings, 0 replies; 6+ messages in thread
From: Neil Jerram @ 2006-09-28  8:50 UTC (permalink / raw)


ludovic.courtes@laas.fr (Ludovic Courtès) writes:

> In short, the lesson is: one should use `make-class' (although
> it's undocumented) rather than `(make <class> ...)' when instantiating a
> new class.

Apologies for not playing earlier, but for the record I agree that
this is the right answer.  The GOOPS code has make-xxx procedures that
can be used programmatically for all the important things (classes,
methods, generics etc.), and the define-xxx macros that we mostly use
are wrappers around these.

One final thought: recommend changing <my-class> to <my-metaclass> (if
you still need it, given make-class).

Regards,
     Neil



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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-09-28  8:50 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-09-26  8:24 GOOPS: Customizing class instantiation Ludovic Courtès
2006-09-26 11:47 ` Ludovic Courtès
2006-09-26 15:26 ` Pat Lasswell
2006-09-27  8:20   ` Ludovic Courtès
2006-09-27 13:16 ` Ludovic Courtès
2006-09-28  8:50   ` Neil Jerram

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).