From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Kevin Ryde Newsgroups: gmane.lisp.guile.devel Subject: doc srfi-9 records Date: Mon, 16 Feb 2004 09:57:26 +1000 Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Message-ID: <87oerzyjyx.fsf@zip.com.au> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1076889648 11062 80.91.224.253 (16 Feb 2004 00:00:48 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 16 Feb 2004 00:00:48 +0000 (UTC) Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Mon Feb 16 01:00:43 2004 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1AsWBm-0008H1-00 for ; Mon, 16 Feb 2004 01:00:43 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.24) id 1AsWAk-00065L-FC for guile-devel@m.gmane.org; Sun, 15 Feb 2004 18:59:38 -0500 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.24) id 1AsW9c-0004dK-B8 for guile-devel@gnu.org; Sun, 15 Feb 2004 18:58:28 -0500 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.24) id 1AsW8p-0003qx-Ft for guile-devel@gnu.org; Sun, 15 Feb 2004 18:58:12 -0500 Original-Received: from [61.8.0.85] (helo=mailout2.pacific.net.au) by monty-python.gnu.org with esmtp (Exim 4.24) id 1AsW8n-0003n0-Aj for guile-devel@gnu.org; Sun, 15 Feb 2004 18:57:37 -0500 Original-Received: from mailproxy1.pacific.net.au (mailproxy1.pacific.net.au [61.8.0.86]) by mailout2.pacific.net.au (8.12.3/8.12.3/Debian-6.6) with ESMTP id i1FNvX5O013202 for ; Mon, 16 Feb 2004 10:57:33 +1100 Original-Received: from localhost (ppp183.dyn10.pacific.net.au [61.8.10.183]) by mailproxy1.pacific.net.au (8.12.3/8.12.3/Debian-6.6) with ESMTP id i1FNvW0H002938 for ; Mon, 16 Feb 2004 10:57:32 +1100 Original-Received: from gg by localhost with local (Exim 3.36 #1 (Debian)) id 1AsW8d-00011G-00; Mon, 16 Feb 2004 09:57:27 +1000 Original-To: guile-devel@gnu.org Mail-Copies-To: never User-Agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3 (gnu/linux) X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Developers list for Guile, the GNU extensibility library List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.lisp.guile.devel:3384 X-Report-Spam: http://spam.gmane.org/gmane.lisp.guile.devel:3384 * srfi-modules.texi (SRFI-9): Revise for detail and clarity. Don't use ":foo" for example type name, since that depends on the keyword reading option. SRFI-9 - define-record-type =========================== This SRFI is a syntax for defining new record types and creating predicate, constructor, and field getter and setter functions. In Guile this is simply an alternate interface to the core record functionality (*note Records::). It can be used with, (use-modules (srfi srfi-9)) - library syntax: define-record-type type (constructor fieldname ...) predicate (fieldname accessor [modifier]) ... Create a new record type, and make various `define's for using it. This syntax can only occur at the top-level, not nested within some other form. TYPE is bound to the record type, which is as per the return from the core `make-record-type'. TYPE also provides the name for the record, as per `record-type-name'. CONSTRUCTOR is bound to a function to be called as `(CONSTRUCTOR fieldval ...)' to create a new record of this type. The arguments are initial values for the fields, one argument for each field, in the order they appear in the `define-record-type' form. The FIELDNAMEs provide the names for the record fields, as per the core `record-type-fields' etc, and are referred to in the subsequent accessor/modifier forms. PREDICTATE is bound to a function to be called as `(PREDICATE obj)'. It returns `#t' or `#f' according to whether OBJ is a record of this type. Each ACCESSOR is bound to a function to be called `(ACCESSOR record)' to retrieve the respective field from a RECORD. Similarly each MODIFIER is bound to a function to be called `(MODIFIER record val)' to set the respective field in a RECORD. An example will illustrate typical usage, (define-record-type employee-type (make-employee name age salary) employee? (name get-employee-name) (age get-employee-age set-employee-age) (salary get-employee-salary set-employee-salary)) This creates a new employee data type, with name, age and salary fields. Accessor functions are created for each field, but no modifier function for the name (the intention in this example being that's set only when an employee object is created). These can all then be used as for example, employee-type => # (define fred (make-employee "Fred" 45 20000.00)) (employee? fred) => #t (get-employee-age fred) => 45 (set-employee-salary fred 25000.00) ;; pay rise The functions created by `define-record-type' are ordinary top-level `define's. They can be redefined or `set!' as desired, exported from a module, etc. _______________________________________________ Guile-devel mailing list Guile-devel@gnu.org http://mail.gnu.org/mailman/listinfo/guile-devel