unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: "Thompson, David" <dthompson2@worcester.edu>
To: 58297@debbugs.gnu.org
Subject: bug#58297: GOOPS slot accessor specialization and inheritance do not compose
Date: Tue, 4 Oct 2022 20:21:51 -0400	[thread overview]
Message-ID: <CAJ=Rwfb3qL_hSvUk=kG66Gq0aGKneH5nZW01YMMF3ksCaQcWKA@mail.gmail.com> (raw)

In Guile, slot accessor specialization and inheritance do not compose.
For example, you can't specialize an accessor's setter for a parent
class and have it apply to a child class.  Every child class defines
new slot accessor methods. which means that the specialized parent
methods will not be called since the new methods take precedence.

The code below demonstrates the issue:

  (use-modules (oop goops))

  (define-class <person> ()
    (name #:init-keyword #:name #:accessor name))

  (define-method ((setter name) (person <person>) new-name)
    (display "renaming!\n")
    (slot-set! person 'name new-name))

  (define-class <child> (<person>))

  (define p1 (make <person> #:name "Alice"))
  (define p2 (make <child> #:name "Bob"))

  ;; Only the first set! call uses the specialized setter method defined
  ;; above.
  (set! (name p1) "Ada")
  (set! (name p2) "Ben")

I would have expected the specialized setter method to apply to both
<person> and <child> since <child> does not shadow the 'name' slot.

I compared this behavior with that of Common Lisp and found that CLOS
does not clobber the method from the parent class, as demonstrated by
this example program that I tested with SBCL:

  (defclass person ()
    ((name :initarg :name :accessor name)))

  (defmethod (setf name) (new-name (obj person))
    (format t "renaming!~&")
    (setf (slot-value obj 'name) new-name))

  (defclass child (person) ())

  (defvar p1 (make-instance 'person :name "Alice"))
  (defvar p2 (make-instance 'child :name "Bob"))

  ;; Both of these setf calls use the specialized setf method defined
  ;; above.
  (setf (name p1) "Ada")
  (setf (name p2) "Ben")

I find the Common Lisp behavior much more desirable.  Is this a bug or
intended behavior?

Thanks for reading,

- Dave





             reply	other threads:[~2022-10-05  0:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-05  0:21 Thompson, David [this message]
2022-10-05 13:26 ` bug#58297: GOOPS slot accessor specialization and inheritance do not compose Mikael Djurfeldt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAJ=Rwfb3qL_hSvUk=kG66Gq0aGKneH5nZW01YMMF3ksCaQcWKA@mail.gmail.com' \
    --to=dthompson2@worcester.edu \
    --cc=58297@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).