unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* Add support for record types in GOOPS methods?
@ 2014-10-21 14:57 Dave Thompson
  2014-10-21 15:37 ` Panicz Maciej Godek
  0 siblings, 1 reply; 3+ messages in thread
From: Dave Thompson @ 2014-10-21 14:57 UTC (permalink / raw)
  To: guile-devel

Hello all,

Last night, I encountered what I consider to be a frustrating limitation
of GOOPS methods: They do not support record type descriptors, only
classes.  This makes it difficult to take advantage of generic
procedures without also buying into the rest of the GOOPS system.

Here's some code that I wish would work:

  (define-record-type <foo>
    (make-foo bar)
    foo?
    (bar foo-bar))
  
  (define-method (foobar (foo <foo>))
    (foo-bar foo))

The error thrown by `define-method' is:

  ERROR: In procedure class-direct-methods:
  ERROR: In procedure slot-ref: Wrong type argument in position 1 (expecting instance): #<record-type <foo>>

There is an ugly workaround.  You can use `class-of' on an instance of a
record type to get a class in return.

This code works, but is unideal:

  (define-record-type <foo>
    (make-foo bar)
    foo?
    (bar foo-bar))

  (define <foo-class> (class-of (make-foo #f)))
  
  (define-method (foobar (foo <foo-class>))
    (foo-bar foo))

I don't know very much about GOOPS, so I am seeking help.  Would it make
sense for `define-method' to work the way I want?  If so, could anyone
suggest a way to make `define-method' to DTRT for record types?  Perhaps
it could auto-generate and cache the class from the record type
descriptor, but I'm not sure how since the current workaround requires
an instance of that record.

Thanks!

-- 
David Thompson
Web Developer - Free Software Foundation - http://fsf.org
GPG Key: 0FF1D807
Support the FSF: https://fsf.org/donate



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

* Re: Add support for record types in GOOPS methods?
  2014-10-21 14:57 Add support for record types in GOOPS methods? Dave Thompson
@ 2014-10-21 15:37 ` Panicz Maciej Godek
  2014-10-21 16:00   ` Thompson, David
  0 siblings, 1 reply; 3+ messages in thread
From: Panicz Maciej Godek @ 2014-10-21 15:37 UTC (permalink / raw)
  To: Dave Thompson; +Cc: guile-devel

[-- Attachment #1: Type: text/plain, Size: 1864 bytes --]

2014-10-21 16:57 GMT+02:00 Dave Thompson <dthompson2@worcester.edu>:

> Hello all,
>
> Last night, I encountered what I consider to be a frustrating limitation
> of GOOPS methods: They do not support record type descriptors, only
> classes.  This makes it difficult to take advantage of generic
> procedures without also buying into the rest of the GOOPS system.
>
> Here's some code that I wish would work:
>
>   (define-record-type <foo>
>     (make-foo bar)
>     foo?
>     (bar foo-bar))
>
>   (define-method (foobar (foo <foo>))
>     (foo-bar foo))
>
> The error thrown by `define-method' is:
>
>   ERROR: In procedure class-direct-methods:
>   ERROR: In procedure slot-ref: Wrong type argument in position 1
> (expecting instance): #<record-type <foo>>
>
> There is an ugly workaround.  You can use `class-of' on an instance of a
> record type to get a class in return.
>
> This code works, but is unideal:
>
>   (define-record-type <foo>
>     (make-foo bar)
>     foo?
>     (bar foo-bar))
>
>   (define <foo-class> (class-of (make-foo #f)))
>
>   (define-method (foobar (foo <foo-class>))
>     (foo-bar foo))
>
> I don't know very much about GOOPS, so I am seeking help.  Would it make
> sense for `define-method' to work the way I want?  If so, could anyone
> suggest a way to make `define-method' to DTRT for record types?  Perhaps
> it could auto-generate and cache the class from the record type
> descriptor, but I'm not sure how since the current workaround requires
> an instance of that record.
>
>
Hi!
As I managed to find out, the (define-record-type t ...) also introduces a
GOOPS class named <t>. Following your example, you'd need to define your
method in the following way:

(define-method (foobar (foo <<foo>>))
    (foo-bar foo))

I don't think any further changes are needed (perhaps a section in the
documentation would be nice)

HTH

[-- Attachment #2: Type: text/html, Size: 3256 bytes --]

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

* Re: Add support for record types in GOOPS methods?
  2014-10-21 15:37 ` Panicz Maciej Godek
@ 2014-10-21 16:00   ` Thompson, David
  0 siblings, 0 replies; 3+ messages in thread
From: Thompson, David @ 2014-10-21 16:00 UTC (permalink / raw)
  To: Panicz Maciej Godek; +Cc: guile-devel

On Tue, Oct 21, 2014 at 11:37 AM, Panicz Maciej Godek
<godek.maciek@gmail.com> wrote:
>
> Hi!
> As I managed to find out, the (define-record-type t ...) also introduces a
> GOOPS class named <t>. Following your example, you'd need to define your
> method in the following way:
>
> (define-method (foobar (foo <<foo>>))
>     (foo-bar foo))
>
> I don't think any further changes are needed (perhaps a section in the
> documentation would be nice)

I had no idea that magic was happening.  Thanks!  That solves my
immediate need, though I think defining something that I didn't
explicitly ask for is strange.

I thought that I could make this technique fail by defining the record
type before importing (oop goops), but it still worked.  Why?

- Dave



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

end of thread, other threads:[~2014-10-21 16:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-10-21 14:57 Add support for record types in GOOPS methods? Dave Thompson
2014-10-21 15:37 ` Panicz Maciej Godek
2014-10-21 16:00   ` Thompson, David

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