[Top][All Lists]

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

GOOPS functional setter

From: Christopher Allan Webber
Subject: GOOPS functional setter
Date: Fri, 13 Jan 2017 13:09:57 -0600
User-agent: mu4e 0.9.18; emacs 25.1.1

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)


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

reply via email to

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