From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Christopher Allan Webber Newsgroups: gmane.lisp.guile.user Subject: GOOPS functional setter Date: Fri, 13 Jan 2017 13:09:57 -0600 Message-ID: <871sw6g4je.fsf@dustycloud.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1484334631 12026 195.159.176.226 (13 Jan 2017 19:10:31 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 13 Jan 2017 19:10:31 +0000 (UTC) User-Agent: mu4e 0.9.18; emacs 25.1.1 To: Guile user Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Fri Jan 13 20:10:24 2017 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cS7Ey-00027y-Ea for guile-user@m.gmane.org; Fri, 13 Jan 2017 20:10:20 +0100 Original-Received: from localhost ([::1]:44787 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cS7F3-0002q3-0D for guile-user@m.gmane.org; Fri, 13 Jan 2017 14:10:25 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58491) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cS7Ei-0002pr-TA for guile-user@gnu.org; Fri, 13 Jan 2017 14:10:06 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cS7Ee-0002N9-M6 for guile-user@gnu.org; Fri, 13 Jan 2017 14:10:04 -0500 Original-Received: from dustycloud.org ([50.116.34.160]:57540) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cS7Ee-0002Lx-GY for guile-user@gnu.org; Fri, 13 Jan 2017 14:10:00 -0500 Original-Received: from oolong (localhost [127.0.0.1]) by dustycloud.org (Postfix) with ESMTPS id E391D2662C for ; Fri, 13 Jan 2017 14:09:57 -0500 (EST) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 50.116.34.160 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:13095 Archived-At: 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 ) 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)