unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* GOOPS functional setter
@ 2017-01-13 19:09 Christopher Allan Webber
  2017-01-13 20:56 ` tomas
  2017-01-13 21:33 ` Jan Nieuwenhuizen
  0 siblings, 2 replies; 8+ messages in thread
From: Christopher Allan Webber @ 2017-01-13 19:09 UTC (permalink / raw)
  To: Guile user

I guess I never sent this to this list.  Here's a functional setter for
GOOPS classes.

Maybe nobody else would use it, but I've used it in a couple of projects
now, so figure I might as well share.

This is LGPLv3+.  Feel free to use.

==================================================

;; By Christopher Allan Webber, LGPLv3+; adapted from shallow-clone in GOOPS
(use-modules (oop goops))

(define-method (slot-fset (self <object>) slot-name value)
  "Return a new copy of SELF, with all slots preserved except SLOT-NAME
set to VALUE."
  (let* ((class (class-of self))
         (clone (allocate-instance class '())))
    (for-each (lambda (slot)
                (define slot-n
                  (slot-definition-name slot))
                (if (and (not (eq? slot-n slot-name)) (slot-bound? self slot-n))
                    (slot-set! clone slot-n (slot-ref self slot-n))))
              (class-slots class))
    ;; Set the particular slot we're overriding
    (slot-set! clone slot-name value)
    clone))

==================================================

(... Would anyone find this useful to include in Guile itself?  It might
also be nice to have a version that sets multiple fields at once, like
set-fields.  I'm not sure what the syntax should look like for that
though.  Maybe keep it simple?)

  (slot-multi-fset foo
    slot-name slot-val
    slot-name2 slot-val2)



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

end of thread, other threads:[~2017-01-15  9:31 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-01-13 19:09 GOOPS functional setter Christopher Allan Webber
2017-01-13 20:56 ` tomas
2017-01-14  2:11   ` Christopher Allan Webber
2017-01-14 10:08     ` tomas
2017-01-14 17:25       ` Arne Babenhauserheide
2017-01-14 21:16       ` Christopher Allan Webber
2017-01-15  9:31         ` tomas
2017-01-13 21:33 ` Jan Nieuwenhuizen

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